在千峰“逆战班”学习的第29天
今天的学习内容是线程的第二种同步方式,线程通信中的wait()和notify()方法以及高级多线程中的线程池
中国加油!武汉加油!千峰加油!我自己加油!!
作业:
8.AC
9.MyThread1中的sleep应该更改为Thread.sleep(),并且在tyr-catch结构中,并且只有实现类缺少线程的创建。MyThread2中的sleep也应该在try-catch中,不能直接抛出
死循环案例:
public class TestDeadLock {
public static void main(String[] args){
LeftChopstick left = new LeftChopstick();
RightChopstick right = new RightChopstick();
Thread boy = new Boy(left,right);
Thread girl = new Girl(left,right);
boy.start();
girl.start();
}
}
class LeftChopstick{
String chopstick = "左筷子";
}
class RightChopstick{
String chopstick = "右筷子";
}
class Boy extends Thread{
LeftChopstick left;
RightChopstick right;
public Boy(){}
public Boy(LeftChopstick left,RightChopstick right){
this.left = left;
this.right = right;
}
public void run(){
System.out.println("男孩要拿筷子");
synchronized(left){
try {
System.out.println("男孩礼让");
left.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("男孩拿起了左筷子,准备拿右筷子");
synchronized(right){
System.out.println("男孩拿起了右筷子,准备吃饭");
}
}
}
}
class Girl extends Thread{
LeftChopstick left;
RightChopstick right;
public Girl(){}
public Girl(LeftChopstick left,RightChopstick right){
this.left = left;
this.right = right;
}
public void run(){
System.out.println("女孩要拿筷子");
synchronized(right){
System.out.println("女孩拿起了右筷子,准备拿左筷子");
synchronized(left){
System.out.println("女孩拿起了左筷子,准备吃饭");
left.notify();
}
}
}
}
运行结果:
生产者和消费者案例:
public class TestProductCustomer {
public static void main(String[] args) {
Shop shop = new Shop();
Thread t1 = new Thread(new Product(shop),"生产者");
Thread t2 = new Thread(new Customer(shop),"消费者");
t1.start();
t2.start();
}
}
class Goods{
private int count;
public Goods(int count){
this.count = count;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
class Shop{
Goods goods;
boolean flag;
public Shop(){}
public Shop(Goods goods,boolean flag){
this.goods = goods;
this.flag = flag;
}
public synchronized void saveGoods(Goods goods) throws InterruptedException{
if(flag == true){
System.out.println("货物充足,等待消费者购买");
this.wait();
}
System.out.println(Thread.currentThread().getName() + "补充了" + goods.getCount() + "件货物");
this.goods = goods;
this.flag = true;
this.notify();
}
public synchronized void buyGoods() throws InterruptedException{
if(flag == false){
System.out.println("缺少货物,消费者等待补充");
this.wait();
}
System.out.println(Thread.currentThread().getName() + "购买了" + goods.getCount() + "件货物");
this.goods = null;
this.flag = false;
this.notify();
}
}
class Product implements Runnable{
Shop shop;
public Product(){}
public Product(Shop shop){
this.shop = shop;
}
public void run(){
for(int i = 1 ; i <= 30; i++){
try {
this.shop.saveGoods(new Goods(i));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Customer implements Runnable{
Shop shop;
public Customer(){}
public Customer(Shop shop){
this.shop = shop;
}
public void run(){
for(int i = 1; i <= 30 ; i++){
try {
this.shop.buyGoods();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
运行结果: