Swap指令實作互斥
public class TestSwap {
public static void main(String[] args) {
Swap s = new Swap();
s.lock[0] = false;
for (int i = 1; i <= 5; i++){
new Producer3(i, s).start();
new Consumer3(i, s).start();
}
}
}
class Consumer3 extends Thread{
private String name = "消費者";
private int num;
private Swap p;
public Consumer3(int num, Swap s) {
this.num = num;
this.p = s;
}
@Override
public void run() {
while(true) {
//P操作
// while(p.full <= 0) {}
p.key[0] = true;
do {
p.swap(p.lock, p.key);
}while(p.key[0] != false);//key為true會一直回圈
//臨界區
// p.empty++;
// p.full--;
// System.out.println(this.name + this.num + "消耗產品后還有:" + p.full);
System.out.println(this.name + this.num +p.key[0]);
//V操作
p.lock[0] = false;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Producer3 extends Thread{
private String name = "生產者";
private int num;
private Swap p;
public Producer3(int num, Swap s) {
this.num = num;
this.p = s;
}
@Override
public void run() {
while(true) {
//P操作
// while(p.empty >= 10) {}
p.key[0] = true;
do {
p.swap(p.lock, p.key);
}while(p.key[0] != false);
//臨界區
// p.empty--;
// p.full++;
// System.out.println(this.name + this.num + "生產產品后還有:" + p.full);
System.out.println(this.name + this.num +p.key[0]);
//V操作
p.lock[0] = false;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//公共變數,便于訪問
class Swap{
//true鎖上
public boolean[] lock = new boolean[1];
public boolean[] key = new boolean[1];
public int full = 0;
public int empty = 10;
public void swap(boolean[] a, boolean[] b){
boolean temp;
temp = a[0];
a[0] = b[0];
b[0] = temp;
}
}
TestandSet指令實作互斥
public class TestTS {
public static void main(String[] args) {
TestAndSet ts = new TestAndSet();
ts.lock[0] = false;
for (int i = 1; i <= 5; i++){
new Producer2(i, ts).start();
new Consumer2(i, ts).start();
}
}
}
class Consumer2 extends Thread{
private String name = "消費者";
private int num;
TestAndSet ts;
public Consumer2(int num, TestAndSet ts) {
this.num = num;
this.ts = ts;
}
@Override
public void run() {
while(true) {
//P操作
while(ts.S <= 0) {}
while(ts.testAndSet(ts.lock)){}
//臨界區
//ts.subS();//ts.getS() <= 0
// if(ts.S > 0) {
// --ts.S;
// System.out.println(this.name + this.num + "消耗產品后還有:" + ts.S);
// }
--ts.S;
System.out.println(this.name + this.num + "消耗產品后還有:" + ts.S);
//V操作
ts.lock[0] = false;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Producer2 extends Thread{
private String name = "生產者";
private int num;
TestAndSet ts;
public Producer2(int num, TestAndSet ts) {
this.num = num;
this.ts = ts;
}
@Override
public void run() {
while(true) {
//P操作
while(ts.S >= 10) {}
while(ts.testAndSet(ts.lock)){}
//臨界區 || ts.getS() >= 10
//ts.addS();
// if(ts.S < 10) {
// ++ts.S;
// System.out.println(this.name + this.num + "生產產品后還有:" + ts.S);
// }
++ts.S;
System.out.println(this.name + this.num + "生產產品后還有:" + ts.S);
//V操作
ts.lock[0] = false;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//公共變數,便于訪問
class TestAndSet{
public boolean[] lock = {false};
public int S = 0;
public synchronized boolean testAndSet(boolean[] lock){
boolean old;
old = lock[0];
lock[0] = true;//true表示資源正在被占用,false表示空閑
return old;
}
uj5u.com熱心網友回復:
key應該是區域變數,while第一個判斷會讀操作寫操作沒互斥,pv操作在這也該互斥轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/282777.html
標籤:Java相關
上一篇:SpringBoot中關于addViewControllers的問題
下一篇:這要怎么解決
