62,628
社区成员
发帖
与我相关
我的任务
分享
public class producter implements Runnable {
private Info info;
public producter(Info info) {
super();
this.info = info;
}
@Override
public void run() {
boolean flag = false;
for (int i = 0; i < 50; i++) {
if (flag) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.info.setNameCntent("小A", "JAVA学员");
flag = false;
} else {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.info.setNameCntent("mol", "www.baidu.com");
flag = true;
}
}
}
}
public class Consumer implements Runnable {
private Info info;
public Consumer(Info info) {
super();
this.info = info;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.info.getNameContent();
}
}
}
public class Info {
private String name = "小A";// 保存信息用
private String content = "JAVA学员";// 保存信息用
private boolean flag = false;
public synchronized void setNameCntent(String name, String Content) {
if (!flag) {
try {
super.wait();// 等待消耗者消耗
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.name = name;
this.content = Content;
flag = false;// 生产完了修改标识
super.notify();
}
}
public synchronized void getNameContent() {
if (flag) {
try {
super.wait();// 等待生产者生产
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(this.name + "---" + this.content);
flag = true;
super.notify();
}
}
}
public class Demo {
public static void main(String[] args) {
Info i = new Info();
producter p = new producter(i);
Consumer c = new Consumer(i);
Thread t = new Thread(p);
Thread t2 = new Thread(c);
t.start();
t2.start();
}
}
public class Info {
private String name = "小A";// 保存信息用
private String content = "JAVA学员";// 保存信息用
private boolean flag = false;
public synchronized void setNameCntent(String name, String Content) {
while (flag) {
try {
wait();// 等待消耗者消耗
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.name = name;
this.content = Content;
flag = true;
notifyAll();
}
public synchronized void getNameContent() {
while(!flag) {
try {
wait();// 等待生产者生产
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(this.name + "---" + this.content);
flag = false;// 生产完了修改标识
notifyAll();
}
}
Producer
public class Producer implements Runnable {
private Info info;
public Producer(Info info) {
this.info = info;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
info.setNameCntent("小A", "JAVA学员");
}
}
}
Consumer
public class Consumer implements Runnable {
private Info info;
public Consumer(Info info) {
super();
this.info = info;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
info.getNameContent();
}
}
}
public class Info {
private String name = "小A";// 保存信息用
private String content = "JAVA学员";// 保存信息用
private boolean flag = false;
public synchronized void setNameCntent(String name, String Content) {
if (!flag) {
flag = true;// 生产完了修改标识
try {
super.wait();// 等待消耗者消耗
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.name = name;
this.content = Content;
super.notify();
}
}
public synchronized void getNameContent() {
if (flag) {
flag = false;
super.notifyAll();;// 等待生产者生产
try {
Thread.sleep(100);
System.out.println(this.name + "---" + this.content);
super.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public synchronized void setNameCntent(String name, String Content) {
if (!flag) {
try {
super.wait();// 等待消耗者消耗
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.name = name;
this.content = Content;
flag = false;// 生产完了修改标识
super.notify();
}
}