有沒有好心人幫忙忙把這個C++代碼改成java代碼?謝謝了
#include"pch.h"
#include<iostream>
#include<Windows.h>
#include<process.h>
#include<time.h>
#include <string>
#include <vector>
using namespace std;
//所謂句柄,就是一個4位元組長的唯一的數,用以標識許多不同的物件型別,句柄是用來標識專案的,這些專案包括:模塊,任務,實體,檔案,記憶體塊,選單,控制,字體,資源,包括圖示,游標,字串等。
HANDLE fullA = 0, fullB = 0;
HANDLE emptyA = 0, emptyB = 0;//剩余空閑區域的數目
HANDLE muteA = 0, muteB;//互斥條件
int sizeA = 10, sizeB = 20, beginA = 5, beginB = 4;//A、B最大容量和初始容量
//string *str=new string[beginA+beginB];
vector<string> va, vb;//vector就是類似于一個陣列的容器,內容比陣列更加全面
int countA = 1, countB = 1;//A、B計數
int times = 10;
DWORD A(LPVOID lm)//double word四個位元組長度,DWORD在Windows下經常用來保存地址(或者存放指標)
{//LPVOID是一個沒有型別的指標,也就是說你可以將LPVOID型別的變數賦值給任意型別的指標
string index = to_string((long long)countA++);
string out;//輸出字串
while (times > 0)
{
times--;
out = "A" + index + "準備取數\n";
cout << out;
::WaitForSingleObject(fullA, INFINITE);//呼叫該函式的執行緒被暫時掛起,當fullA為有信號量狀態時接觸掛起
::WaitForSingleObject(muteA, 100);//當muteA有信號量或時間大于100毫秒時解除掛起
int getnum = ::rand() % 100 * beginA / 100;//rand()不需要引數,它會回傳一個從0到最大亂數的任意整數,最大亂數的大小通常是固定的一個大整數。
//產生0~99這100個整數中的一個隨機整數
string getstr = va[getnum];
out = "A" + index + "取數:" + getstr + "\n";
cout << out;
//printf("A%d取數:%s\n",index,&getstr);
va.erase(va.begin() + getnum);//洗掉這個位置的字符
beginA--;
::ReleaseSemaphore(muteA, 1, NULL);
out = "A" + index + "處理:" + getstr + "->" + getstr + "|A" + index + "\n";
cout << out;
//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 + "處理完畢," + getstr + "放入B\n";
cout << out;
vb.push_back(getstr);
beginB++;
::ReleaseSemaphore(muteB, 1, NULL);
::ReleaseSemaphore(fullB, 1, NULL);
}
out = "執行緒A" + index + "結束\n";
cout << out;
return 0;
}
DWORD B(LPVOID lm)
{
string index = to_string((long long)countB++);
string out;
while (times > 0)
{
times--;
out = "B" + index + "準備取數\n";
cout << out;
::WaitForSingleObject(fullB, INFINITE);
::WaitForSingleObject(muteB, 1000);
int getnum = ::rand() % 100 * beginB / 100;
string getstr = vb[getnum];
out = "B" + index + "取數:" + getstr + "\n";
cout << out;
vb.erase(vb.begin() + getnum);
beginB--;
::ReleaseSemaphore(muteB, 1, NULL);
out = "B" + index + "處理:" + getstr + "->" + getstr + "|B" + index + "\n";
cout << out;
getstr = getstr + "|B" + index;
::WaitForSingleObject(emptyA, 1000);
::WaitForSingleObject(muteA, 1000);
out = "B" + index + "處理完畢," + getstr + "放入A\n";
cout << out;
va.push_back(getstr);
beginA++;
::ReleaseSemaphore(muteA, 1, NULL);
::ReleaseSemaphore(fullA, 1, NULL);
}
out = "執行緒B" + index + "結束\n";
cout << out;
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<string>(beginA, "A");
vb = vector<string>(beginB, "B");
HANDLE hThread[1] = { 0 };//句柄
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 << "運行結束,輸出各條資料:\n" << "A:\n";
for (int i = 0; i < beginA; i++)
cout << va[i] << endl;
cout << "B:\n";
for (int i = 0; i < beginB; i++)
cout << vb[i] << endl;
cin >> times;
for (int i = 0; i < 3; i++)
{
::CloseHandle(hThread[i]);
}
::CloseHandle(emptyA);
::CloseHandle(emptyB);
::CloseHandle(fullA);
::CloseHandle(fullB);
::CloseHandle(muteA);
::CloseHandle(muteB);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/122758.html
標籤:數據結構與算法
下一篇:做個美麗的程式媛
