一、奇数偶数
1.Date
public class Date { private int num;
public int getNum() {
return num;
} public void setNum(int num) {
this.num = num;
} public Date(int num) {
super();
this.num = num;
}
public synchronized void Even() {
this.notifyAll();
if(num%2==0) {
System.out.println("Even"+Thread.currentThread().getName()+"-->"+(num++));
try {
Thread.sleep(1000);
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public synchronized void Odd() {
this.notifyAll();
if(num%2!=0) {
System.out.println("Odd"+Thread.currentThread().getName()+"-->"+(num++));
try {
Thread.sleep(1000);
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
2.AThread
public class AThread implements Runnable { private Date date;
public AThread(Date date) {
super();
this.date = date;
} @Override
public void run() {
while(true) {
date.Even();
}
}
}
3.BThread
public class BThread implements Runnable { private Date date;
public BThread(Date date) {
super();
this.date = date;
} @Override
public void run() {
while(true) {
date.Odd();
}
}}
4.Test
public class Test { public static void main(String[] args) {
Date date=new Date(0);
AThread a=new AThread(date);
BThread b=new BThread(date);
Thread t1=new Thread(a);
Thread t2=new Thread(b);
t1.start();
t2.start();
}
}
二、生产者,消费者
1.Food
package com.pro.food;
public class Food { private String name;
private String type;
private int j;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getJ() {
return j;
}
public void setJ(int j) {
this.j = j;
}
public Food(int j) {
super();
this.j = j;
}
}
2.Product
package com.pro.food;
public class Product implements Runnable { private Food food;
private int i=0;
public Product(Food food) {
super();
this.food = food;
} @Override
public void run() {
while(true) {
synchronized(food) {
food.notifyAll();
if(food.getJ()==0) {
if(i%2==0) {
food.setType("炒饭");
food.setName("蛋炒饭");
food.setJ(1);
}else if(i%2!=0) {
food.setType("包子");
food.setName("肉包");
food.setJ(1);
}
i++;
System.out.println("生产了"+food.getType()+"-"+food.getName());
}else if(food.getJ()==1) {
try {
Thread.sleep(1000);
food.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} }}
3.Consumer
package com.pro.food;
public class Consumer implements Runnable { private Food food;
public Consumer(Food food) {
super();
this.food = food;
} @Override
public void run() {
while(true) {
synchronized (food) {
food.notifyAll();
if(food.getJ()==1) {
System.out.println("拿到了"+food.getType()+"-"+food.getName());
food.setJ(0);
}else if(food.getJ()==0) {
try {
Thread.sleep(1000);
food.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} }}
【多线程,奇数偶数,生产者,消费者】4.Test
package com.pro.food;
public class Test { public static void main(String[] args) {
Food food=new Food(0);
Product pro=new Product(food);
Consumer con=new Consumer(food);
Thread t1=new Thread(pro);
Thread t2=new Thread(con);
t1.start();
t2.start();
}
}
推荐阅读
- Java|Java基础——数组
- 人工智能|干货!人体姿态估计与运动预测
- java简介|Java是什么(Java能用来干什么?)
- Java|规范的打印日志
- Linux|109 个实用 shell 脚本
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- Spring注解驱动第十讲--@Autowired使用
- SqlServer|sql server的UPDLOCK、HOLDLOCK试验
- jvm|【JVM】JVM08(java内存模型解析[JMM])
- 技术|为参加2021年蓝桥杯Java软件开发大学B组细心整理常见基础知识、搜索和常用算法解析例题(持续更新...)