java简单线程间通信

以前学多线程时没有好好学,现在回来补一补,打算先跟着书上敲一个简单的进程间通信的例子

一个生产者,一个消费者,生产者生产产品放在仓库(goods),消费者从仓库消费产品,仓库最多存放一件产品(最简单的情况),但是必须要先生产才能消费,这就需要进程间的通信。先上代码:

package ThreadTest;


import java.util.ArrayList;
import java.util.List;

public class FirstThread {
    
    
	public static void main(String[] args) throws InterruptedException {
    
    
		List<Object> goods = new ArrayList<>();	//储存物品的仓库,最多储存1
		Thread thread1 = new Thread(()->{
    
    		//线程1,生产产品
			int num = 0;
			while(true) {
    
    
				synchronized (goods) {
    
    
					if(goods.size()==0) {
    
     
						goods.add("商品" + ++num);
						System.out.println("生产者生产了第"+num+"个产品");
					}
					else if(goods.size()>0)
						try {
    
    
							goods.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();
				}
			}
		}, "生产者");
		Thread thread2 = new Thread(()->{
    
    
			int num = 0;
			while(true) {
    
    
				synchronized (goods) {
    
    
					if(goods.size()>0) {
    
    
						goods.remove("商品"+ ++num);
						System.out.println("消费者消费了第"+num+"个产品");
					}
					else if (goods.size()==0)
						goods.notify();;
				}
				try {
    
    
					Thread.sleep(100);
				} catch (InterruptedException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}, "消费者");
		thread1.start();
		thread2.start();
	}
}

‘synchronized ’这个单词确实是很难记住,书上说Lock类能实现和‘synchronized ’一样的作用并且功能更加强大,但是在这里我不知道怎么用Lock,Lock没有传入任何对象,所以各个线程无法知道商品的实时情况,但‘synchronized ’却可以把goods传入。

这里的代码顺序一定要注意,稍有不同可能结果就不一样,例如我之前的输出的语句位置不同,在结果中就会出现多个相同的输出,原来的代码是这样的:

package ThreadTest;


import java.util.ArrayList;
import java.util.List;

public class FirstThread {
    
    
	public static void main(String[] args) throws InterruptedException {
    
    
		List<Object> goods = new ArrayList<>();	//储存物品的仓库,最多储存1
		Thread thread1 = new Thread(()->{
    
    		//线程1,生产产品
			int num = 0;
			while(true) {
    
    
				synchronized (goods) {
    
    
					if(goods.size()==0) goods.add("商品" + ++num);
					else if(goods.size()>0)
						try {
    
    
							goods.wait();
						} catch (InterruptedException e) {
    
    
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					System.out.println("生产者生产了第"+num+"个产品");
					
				}
				try {
    
    
					Thread.sleep(100);
				} catch (InterruptedException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}, "生产者");
		Thread thread2 = new Thread(()->{
    
    
			int num = 0;
			while(true) {
    
    
				synchronized (goods) {
    
    
					if(goods.size()>0) goods.remove("商品"+ ++num);
					else if (goods.size()==0)
						goods.notify();
					System.out.println("消费者消费了第"+num+"个产品");
					
				}
				try {
    
    
					Thread.sleep(100);
				} catch (InterruptedException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}, "消费者");
		thread1.start();
		thread2.start();
	}
}

然后回出现这样的情况
在这里插入图片描述
具体情况等我弄清楚了再回来写

Guess you like

Origin blog.csdn.net/m0_45972156/article/details/116276749