Java 生产者和消费者问题
来源:互联网 发布:天猫 淘宝商城长靴 编辑:程序博客网 时间:2024/06/05 07:40
运行结果:
code:
class Info {
private String name = "Carter";
private int age = 25;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class Producer implements Runnable {
private Info info = null;
Producer(Info info) {
this.info = info;
}
public void run() {
boolean flag = false;
int i;
for(i=1;i<30;i++) {
if (flag) {
this.info.setName("Carter");
try {
Thread.sleep(100);
} catch(Exception e) {
e.printStackTrace();
}
this.info.setAge(25);
flag = false;
} else {
this.info.setName("Steven");
try {
Thread.sleep(100);
} catch(Exception e) {
e.printStackTrace();
}
this.info.setAge(100);
flag = true;
}
}
}
}
class Consumer implements Runnable {
private Info info = null;
public Consumer(Info info) {
this.info = info;
}
public void run() {
int i;
for(i=1; i<25;i++) {
try {
Thread.sleep(100);
} catch(Exception e) {
e.printStackTrace();
}
System.out.println(this.info.getName() + " <======> " + this.info.getAge());
}
}
}
public class ThreadTest {
public static void main(String[] args) {
Info info = new Info();
Producer pro = new Producer(info);
Consumer con = new Consumer(info);
new Thread(pro).start();
new Thread(con).start();
}
}
等待和唤醒的操作:
code:
class Info {
private String name = "Carter";
private int age = 25;
private boolean flag=false;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public synchronized void set(String name, int age) {
if (!flag) {
try {
super.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
this.name = name;
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
this.age = age;
flag = false;
super.notify();
}
public synchronized void get() {
if (flag) {
try {
super.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(this.getName() + " <===================>" + this.getAge());
flag = true;
super.notify();
}
}
class Producer implements Runnable {
private Info info = null;
Producer(Info info) {
this.info = info;
}
public void run() {
boolean flag = false;
int i;
for(i=1;i<25;i++) {
if (flag) {
this.info.set("Carter", 20);
flag = false;
} else {
this.info.set("Steven",100);
flag = true;
}
}
}
}
class Consumer implements Runnable {
private Info info = null;
public Consumer(Info info) {
this.info = info;
}
public void run() {
int i;
for(i=1; i<25;i++) {
try {
Thread.sleep(100);
} catch(Exception e) {
e.printStackTrace();
}
this.info.get();
}
}
}
public class ThreadTest {
public static void main(String[] args) {
Info info = new Info();
Producer pro = new Producer(info);
Consumer con = new Consumer(info);
new Thread(pro).start();
new Thread(con).start();
}
}
- Java 生产者和消费者问题
- Java生产者和消费者问题
- java生产者和消费者问题
- java生产者和消费者问题
- 用java实现生产者和消费者问题
- 生产者和消费者问题+java版
- Java实现生产者和消费者问题
- 用java实现生产者和消费者问题
- JAVA线程中的生产者和消费者问题
- 生产者和消费者问题的Java实现
- Java中,生产者和消费者的问题
- java学习之消费者和生产者问题
- java生产者 消费者问题
- Java生产者消费者问题
- java生产者消费者问题
- Java 生产者消费者问题
- java 生产者消费者问题
- Java生产者消费者问题 .
- hadoop文件输出控制,多路径输出到不同文件
- aix系挂载光驱和iso相关命令
- 典型序列及信源编码的一种理解
- JavaScript:函数的可选参数
- netgen.5.0.0下载地址与Windows下编译方法
- Java 生产者和消费者问题
- Score
- Linux下递归创建目录
- unity3d_协同进程研究(1)
- C语言--输出打印表格
- cuda by example 的代码的问题
- 纯真IP数据库格式详解
- DataNode2: java.io.IOException: Could not find my address: datanode2 in list of ZooKeeper quorum ser
- 内存分配(mallloc,calloc,realloc,new)