62,628
社区成员
发帖
与我相关
我的任务
分享package CoTest;
/**
* 信号灯
* 借助标志位
* @author yuche
*
*/
public class CoTest2 {
public static void main(String[] args) {
Bread1 bread=new Bread1();
new Producer1(bread).start();
new Consumer1(bread).start();
}
}
class Consumer1 extends Thread{
Bread1 bread;
public Consumer1(Bread1 bread) {
super();
this.bread = bread;
}
@Override
public void run() {
for(int i=1;i<100;++i) {
consume();
}
}
public synchronized void consume(){
if(bread.flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("消费");
bread.flag=!bread.flag;
this.notifyAll();
}
}
class Producer1 extends Thread{
Bread1 bread;
public Producer1(Bread1 bread) {
super();
this.bread = bread;
}
@Override
public void run() {
for(int i=1;i<100;++i) {
produce();
}
}
public synchronized void produce(){
if(!bread.flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("生产");
bread.flag=!bread.flag;
this.notifyAll();
}
}
//资源
class Bread1{
//为T表示面包在生产,为F表示可以消费了
boolean flag;
public Bread1() {
flag=true;
}
}

public class CoTest2 {
public static void main(String[] args) {
Bread1 bread = new Bread1();
new Producer1(bread).start();
new Consumer1(bread).start();
}
}
class Consumer1 extends Thread {
Bread1 bread;
public Consumer1(Bread1 bread) {
super();
this.bread = bread;
}
@Override
public void run() {
for (int i = 1; i < 100; ++i) {
bread.consume();
}
}
}
class Producer1 extends Thread {
Bread1 bread;
public Producer1(Bread1 bread) {
super();
this.bread = bread;
}
@Override
public void run() {
for (int i = 1; i < 100; ++i) {
bread.produce();
}
}
}
//资源
class Bread1 {
// 为T表示面包在生产,为F表示可以消费了
boolean flag;
public Bread1() {
flag = true;
}
public synchronized void consume() {
if (flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("消费");
flag = !flag;
this.notifyAll();
}
public synchronized void produce() {
if (!flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("生产");
flag = !flag;
this.notifyAll();
}
}
[/quote]明白了,感谢大神
public class CoTest2 {
public static void main(String[] args) {
Bread1 bread = new Bread1();
new Producer1(bread).start();
new Consumer1(bread).start();
}
}
class Consumer1 extends Thread {
Bread1 bread;
public Consumer1(Bread1 bread) {
super();
this.bread = bread;
}
@Override
public void run() {
for (int i = 1; i < 100; ++i) {
bread.consume();
}
}
}
class Producer1 extends Thread {
Bread1 bread;
public Producer1(Bread1 bread) {
super();
this.bread = bread;
}
@Override
public void run() {
for (int i = 1; i < 100; ++i) {
bread.produce();
}
}
}
//资源
class Bread1 {
// 为T表示面包在生产,为F表示可以消费了
boolean flag;
public Bread1() {
flag = true;
}
public synchronized void consume() {
if (flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("消费");
flag = !flag;
this.notifyAll();
}
public synchronized void produce() {
if (!flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("生产");
flag = !flag;
this.notifyAll();
}
}