Home > Back-end >  Operating system simulation hardware synchronization mechanism inside, but power is still light can&
Operating system simulation hardware synchronization mechanism inside, but power is still light can&

Time:05-05

Swap command implementation incompatible
Public class TestSwap {
Public static void main (String [] args) {
Swap s=new Swap ();
S.l ock [0]=false;
For (int I=1; I & lt;=5; I++) {
New Producer3 (I, s). The start ();
New Consumer3 (I, s). The start ();
}
}
}

The class Consumer3 extends Thread {
Private String name="consumers";
Private int num.
Private Swap p;
Public Consumer3 (int num, Swap s) {
this.num=num;
This. P=s;
}

@ Override
Public void the run () {
While (true) {
//P
//while (p.f ull & lt;=0) {}
P.k ey [0]=true;
Do {
P. wap (p.l ock, p.k ey);
} while (p.k ey [0]!=false);//the key to true will always cycle
//critical section
//p.e mpty++;
//p.f ull -;
//System. Out. Println (enclosing the name + enclosing num + "consumption products have: after" + p.f ull);
System. The out. Println (enclosing the name + enclosing num + p.k ey [0]).
//V
P.l ock [0]=false;
Try {
Thread.sleep (2000);
{} catch InterruptedException (e)
e.printStackTrace();
}
}
}
}

The class Producer3 extends Thread {
Private String name="producers";
Private int num.
Private Swap p;
Public Producer3 (int num, Swap s) {
this.num=num;
This. P=s;
}

@ Override
Public void the run () {
While (true) {
//P
//while (p.e mpty & gt;=10) {}
P.k ey [0]=true;
Do {
P. wap (p.l ock, p.k ey);
} while (p.k ey [0]!=false);
//critical section
//p.e mpty -;
//p.f ull++;
//System. Out. Println (enclosing the name + enclosing num + "production products have: after" + p.f ull);
System. The out. Println (enclosing the name + enclosing num + p.k ey [0]).
//V
P.l ock [0]=false;
Try {
Thread.sleep (2000);
{} catch InterruptedException (e)
e.printStackTrace();

}
}
}
}

//public variables, facilitate access to
The class Swap {
//true lock
Public Boolean lock []=new Boolean [1].
Public Boolean [] key=new Boolean [1].
Public int full=0;
Public int the empty=10;
Public void swap (Boolean [] a, Boolean [] b) {
Boolean temp.
Temp=a, [0].
[0] a [0]=b;
B [0]=temp;
}
}

TestandSet instruction implement mutual exclusion

Public class TestTS {
Public static void main (String [] args) {
TestAndSet ts=new TestAndSet ();
Ts. The lock [0]=false;
For (int I=1; I & lt;=5; I++) {
Ts, new Producer2 (I) start ();
Ts, new Consumer2 (I) start ();
}
}
}

The class Consumer2 extends Thread {
Private String name="consumers";
Private int num.
TestAndSet ts;
Public Consumer2 (int num, TestAndSet ts) {
this.num=num;
This. Ts=ts;
}

@ Override
Public void the run () {
While (true) {
//P
While (ts) S & lt;=0) {}
While (ts) testAndSet (ts. Lock)) {}
//critical section
//ts. SubS ();//ts. GetS () & lt;=0
//if (ts) S & gt; 0 {
//-- -- ts. S;
//System. Out. Println (enclosing the name + enclosing num + "consumption products have: after" + ts. S);
//}
- ts. S;
System. The out. Println (enclosing the name + enclosing num + "consumption products have: after" + ts. S);
//V
Ts. The lock [0]=false;
Try {
Thread.sleep (2000);
{} catch InterruptedException (e)
e.printStackTrace();
}

}
}
}

The class Producer2 extends Thread {
Private String name="producers";
Private int num.
TestAndSet ts;
Public Producer2 (int num, TestAndSet ts) {
this.num=num;
This. Ts=ts;
}

@ Override
Public void the run () {
While (true) {
//P
While (ts. S & gt;=10) {}
While (ts) testAndSet (ts. Lock)) {}
//critical region | | ts. GetS () & gt;=10
//ts. AddS ();
//if (ts) S & lt; 10) {
//+ + ts. S;
//System. Out. Println (enclosing the name + enclosing num + "production products have: after" + ts. S);
//}
+ + ts. S;
System. The out. Println (enclosing the name + enclosing num + "production products have: after" + ts. S);
//V
Ts. The lock [0]=false;
Try {
Thread.sleep (2000);
{} catch InterruptedException (e)
e.printStackTrace();

}
}
}
}


//public variables, facilitate access to
The class TestAndSet {
Public Boolean [] lock={false};
Public int S=0;
Public synchronized Boolean testAndSet (Boolean lock []) {
Boolean old;
Old=lock [0];
The lock [0]=true;//true said resources are being used, false said free
Return old;
}

CodePudding user response:

Key is supposed to be a local variable, while the first judgement can read write operation not mutually exclusive, pv operation in which the mutex
  • Related