• 主页
  • Java SE
  • J2ME
  • Web开发
  • Java EE
  • Eclipse

操作系统里面模拟硬件同步机制,但是功力尚浅锁不了,不知道是怎么回事,望提点一番。

搭车去柏林<‘^′> 福建师范大学 软件工程  2021-05-04 02:57:08
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;
}
...全文
16 点赞 收藏 1
写回复
1 条回复
key应该是局部变量,while第一个判断会读操作写操作没互斥,pv操作在这也该互斥
回复 点赞
发动态
发帖子
Java
创建于2007-09-28

1.8w+

社区成员

8.1w+

社区内容

Java相关技术讨论
社区公告
暂无公告