Home > other >  C code into Java code
C code into Java code

Time:10-10

Is there a good intention person to help this c + + code into Java code? Thank you
# include "PCH. H"
#include
#include
#include
#include
# include & lt; String>
# include & lt; Vector>
using namespace std;
//handle to the so-called, is a unique number, 4 bytes long identifies a number of different object types, handle is used to identify projects, these projects include: module, task instance, files, memory blocks, menu, control, font, resources, including ICONS, cursors, string, etc.,
HANDLE fullA=0, fullB=0;
HANDLE emptyA=0, emptyB=0;//the number of the remaining free area
HANDLE muteA=0, muteB;//the mutex condition
Int sizeA=10, sizeB=20, beginA=5, beginB=4;//A, B, the maximum capacity and initial capacity
//STR=new string string * [beginA + beginB];
Vector Va, vb.//vector is similar to an array of container, content more comprehensive than array
Int countA=1, countB=1;//A, B, count
Int times=10;
DWORD A (LPVOID lm)//double word length of four bytes, dwords under Windows often deposit (or pointer) is used to store address
{//LPVOID is a type of pointer, which means you can be LPVOID type variable assignment for any type of pointer
String index=to_string ((long long) countA++);
String out;//output string
While (times & gt; 0)
{
Times --;
Out="A" + index + "ready to take several \ n";
Cout & lt; : : WaitForSingleObject (fullA, INFINITE);//call the function of thread is temporarily suspended, when fullA as the state of a semaphore contact hang
: : WaitForSingleObject (muteA, 100);//when muteA is terminated when a semaphore or time is greater than 100 milliseconds hang
Int getnum=: : rand () % 100 * beginA/100;//rand () does not need parameter, it returns a random integer from 0 to the maximum random number, the size of the largest random number is usually fixed a large integer,
//produce 0 ~ 99 a random integer in the integer 100
String getstr=va [getnum];
Out="A" + index + "access:" + getstr + "\ n";
Cout & lt; //printf (" % d take A count: % s \ n ", index, & amp; Getstr);
Va. Erase (va. The begin () + getnum);//remove the position of characters
BeginA -;

: : ReleaseSemaphore (muteA, 1, NULL);
Out="A" + index + ":" + getstr + "- & gt;" + getstr + "| A" + index + "\ n";
Cout & lt; //printf (" A % d: % s> % s, A % d \ n ", index, getstr, getstr, index);
Getstr=getstr + "| A" + index;
: : WaitForSingleObject (emptyB, 100);
: : WaitForSingleObject (muteB, 100);
Out="A" + index + "processed," + getstr + "into the B \ n";
Cout & lt; Vb. The push_back (getstr);
BeginB++;
: : ReleaseSemaphore (muteB, 1, NULL);
: : ReleaseSemaphore (fullB, 1, NULL);

}
Out="thread A" + index + "end \ n";
Cout & lt; return 0;
}
DWORD B (LPVOID lm)
{
String index=to_string ((long long) countB++);
String out;
While (times & gt; 0)
{
Times --;
Out="B" + index + "ready to take several \ n";
Cout & lt; : : WaitForSingleObject (fullB, INFINITE);
: : WaitForSingleObject (muteB, 1000);
Int getnum=: : rand () % 100 * beginB/100;
String getstr=vb [getnum];
Out="B" + index + "access:" + getstr + "\ n";
Cout & lt; Vb. Erase (vb. The begin () + getnum);
BeginB -;

: : ReleaseSemaphore (muteB, 1, NULL);
Out="B" + index + ":" + getstr + "- & gt;" + getstr + "|" B "+ index +" \ n ";
Cout & lt; Getstr=getstr + "|" B + index;
: : WaitForSingleObject (emptyA, 1000);
: : WaitForSingleObject (muteA, 1000);
Out="B" + index + "processed," + getstr + "into A" \ n;
Cout & lt; Va. Push_back (getstr);
BeginA++;
: : ReleaseSemaphore (muteA, 1, NULL);
: : ReleaseSemaphore (fullA, 1, NULL);
}
Out="thread B" + index + "end \ n";
Cout & lt; return 0;

}

Int main ()
{
: : srand ((int) time (0));
EmptyA=: : CreateSemaphoreA (NULL, sizeA - beginA sizeA, NULL);
EmptyB=: : CreateSemaphoreA (NULL, sizeB - beginB sizeB, NULL);
FullA=: : CreateSemaphoreA (NULL, beginA sizeA, NULL);
FullB=: : CreateSemaphoreA (NULL, beginB sizeB, NULL);
MuteA=: : CreateSemaphoreA (NULL, 1, 1, NULL);
MuteB=: : CreateSemaphoreA (NULL, 1, 1, NULL);
Va=vector (beginA, "A");
Vb=vector (beginB, "B");
HANDLE hThread [1]={0};//handle
HThread [0]=: : CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE). A, NULL, 0, NULL);
HThread [1]=: : CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) B, NULL, 0, NULL);
HThread [2]=: : CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE). A, NULL, 0, NULL);
: : WaitForMultipleObjects (3, hThread, true, INFINITE).
Cout & lt; <"End of the run, output various data: \ n" & lt; <"A: \ n";
for (int i=0; I & lt; BeginA; I++)
Cout & lt; Cout & lt; <"B: \ n";
for (int i=0; I & lt; BeginB; I++)
Cout & lt; Cin & gt;> Times;
for (int i=0; I & lt; 3; I++)
{
: : CloseHandle (hThread [I]);
}
: : CloseHandle (emptyA);
: : CloseHandle (emptyB);
: : CloseHandle (fullA);
: : CloseHandle (fullB);
: : CloseHandle (muteA);
: : CloseHandle (muteB);

}

  • Related