Class ThreadTest {
static int type = 4,num = 10; //Define the number of resources and threads.
static int[]resource = new int[type]; //Total system resources
//static int[]copy resource = new int[type]; //copy
Static random rand = newrandom ();
Static Bank [] Bank = New Bank [No.]; //thread group
Bank temp = new Bank();
public void init() {
//Initialize each thread in the group and randomly fill in the total system resources.
for(int I = 0; I< type; i++)
resource[I]= rand . nextint( 10)+80;
system . out . print(" Resource:");
for(int I = 0; I< type; i++)
system . out . print("+resource[I]);
system . out . println(" ");
for(int I = 0; I< Bank. Length; i++)
Bank[i] = new bank ("#"+i);
}
Common Thread Test 4() {
init();
}
Class library extension thread {
//Banker algorithm to avoid deadlock
Public int[]
Max = new int[type],//total demand
Need = new int[type],//the amount of resources still needed.
allocation = new int[type]; //Allocated quantity
Private int[]
Request = new int[type],//the amount of resources requested.
copy resource = new int[type]; //resource replication
Private boolean isFinish = false// Is the thread finished?
int[][]table = new int[bank . length][type * 4]; //Two-dimensional resource allocation table
Private void init() {
//Fill in the total quantity, remaining quantity and allocated quantity randomly.
Synchronization (resource) {
for(int I = 0; I< type; i++) {
max[I]= rand . nextint(5)+ 10;
need[I]= rand . nextint( 10);
Allocation [i] = maximum [i]-need [I];
Resources [i] -= allocation [I]; //Subtract the allocated resources from the system resources
}
printer();
for(int I = 0; I< type; i++) {
if(resource[I]& lt; 0) {
//If there is an error that the allocated quantity exceeds the total system resources, exit.
System.out.println ("Thread allocation sum is out of range!" );
system . exit( 1);
}
}
}
}
Public Bank (String) (
Collection name;
init();
start();
}
Public bank () {
//None
}
Public invalid operation () {
Try {
Sleep (rand. nextint(2000)););
}
Catch (interrupt exception e)
Throw a new runtimeexception (e);
}
while(true) {
//Continue to apply for resources without completing the procedure.
if(askFor() == false) {
Try {
Sleep (1000);
}
Catch (interrupt exception e)
Throw a new runtimeexception (e);
}
}
other
try request();
if(noNeed() == true)
Break;
}
//Sleep for a period of time, and the simulation program runs.
Try {
Sleep (1000);
}
Catch (interrupt exception e)
Throw a new runtimeexception (e);
}
system . out . println(getName()+" finish!" );
Synchronization (resource) {
//Release the occupied resources after running.
for(int I = 0; I< type; i++) {
Resources [i] += allocation [I];
need[I]= allocation[I]= max[I]= 0;
}
}
}
Private void printer () {
//Print the current resource information
system . out . print(getName()+" Max:");
for(int I = 0; I< type; i++)
system . out . print("+max[I]);
system . out . print(" Allocation:");
for(int I = 0; I< type; i++)
system . out . print("+allocation[I]);
system . out . print(" Need:");
for(int I = 0; I< type; i++)
system . out . print("+need[I]);
system . out . print(" Available:");
for(int I = 0; I< type; i++)
system . out . print("+resource[I]);
system . out . println(" ");
}
Private Boolean askFor() {
//Randomly generate the applied resource quantity, and check whether it exceeds the standard.
Boolean canAsk = false
for(int I = 0; I< type; i++) {
request[I]= rand . nextint(20);
//Prevent the application from exceeding the required amount.
if(request[I]& gt; Need [me])
Request [i] = need [i];
}
for(int I = 0; I< type; I++) // All resources for preventing random application are 0.
if(request[I]& gt; 0)
canAsk = true
Synchronization (resource) {
//Lock the available resources and check whether they exceed the standard.
for(int I = 0; I< type; i++) {
if(request[I]& gt; Resources [i])
//If the applied resources exceed the available resources, please wait for some time and reapply.
Returns false
}
}
Return to canAsk
}
Private void tryRequest() {
//Create a copy and try to allocate the request.
Synchronization (resource) {
for(int I = 0; I< type; i++)
//Still want to prevent the number of requests from exceeding the scope.
if(request[I]& gt; Resources [i])
Return;
for(int I = 0; I< type; i++) {
//Copy the resource quantity and subtract the required quantity to get a copy.
copy resource[I]= resource[I];
copy resource[I]-= request[I];
}
system . out . print(getName()+" ask for:");
for(int I = 0; I< type; i++)
system . out . print("+request[I]);
system . out . println(" ");
if(checkSafe() == true) {
//If the security is checked, assign the copy value to the resource quantity, and modify the occupied quantity and the required quantity.
for(int I = 0; I< type; i++) {
resource[I]= copy resource[I];
Allocation [i] += request [I];
need[I]-= request[I];
}
System.out.println(getName()+"Request succeeded!" );
}
other
System.out.println(getName()+"Request failed!" );
}
}
Private Boolean checkSafe() {
//Banker algorithm checks security.
Synchronization (Bank) {
//Put the thread resource information into the two-dimensional resource allocation table to check the security. 0~type available resources /type ~ type * 2 required resources /type * 2 ~ type * 3 occupied resources /type * 3 ~- 1 available+occupied resources.
for(int I = 0; I< Bank. Length; i++) {
for(int j = type; J< type * 2; j++) {
Table [i][j] = Bank [i]. [j% type] is required;
}
for(int j = type * 2; J< type * 3; j++) {
Table [i][j] = Bank [i]. Assign [j% type];
}
}
//Bubble sorting is arranged according to the demand resources from small to large.
for(int I = 0; I< Bank. Length; i++) {
for(int j = I; j & ltbank . length- 1; j++) {
sort(j,4);
}
}
//Conduct safety inspection at this time.
for(int I = 0; I< type; i++) {
table[0][I]= copy resource[I];
Table [0][I+ type *3] = table [0][i]+table [0][I+ type * 2];
if(table[0][I+type * 3]& lt; Table [1][I+ type])
Returns false
}
for(int j = 1; j & ltbank . length- 1; j++) {
for(int k = 0; K< type; k++) {
Table [j][k] = table [j-1] [k+type * 3];
Table [j][k+type*3] = table [j][k]+table [j] [k+type * 2];
if(table[j][k+type * 3]& lt; Table [j+ 1][k+ type])
Returns false
}
}
}
Return true
}
Private empty sort (int j, int k) {
//Recursive bubble sorting
int tempNum
if(table[j][k]& gt; Table [j+1] [k]) (
for(int I = type; I< type * 2; i++) {
tempNum = table[j][I];
Table [j][i] = table [j+1] [i];
table[j+ 1][I]= tempNum;
}
/* temp = bank[j];
bank[j]= bank[j+ 1];
bank[j+ 1]= temp; */
}
else if(table[j][k]= = table[j+ 1][k]& amp; & amp < type * 2)//When this resource amount is the same, recursively sort the next resource amount to prevent it from exceeding the scope.
sort(j,k+ 1);
}
Private boolean noNeed() {
//Do you still need resources?
Boolean completion = true;
for(int I = 0; I< type; i++) {
If (need [me]! = 0) {
finish = false
Break;
}
}
Return to completion;
}
}
Public static void main(String[] args) {
ThreadTest t = new ThreadTest();
//Background thread, which sets how long the program will run and automatically end.
New timeout (30000, "-Stop! ! ! - ");
}
}