线程间的几种通信方式

短线程间的通信有两种模型:消息传递和内存共享,基本上的实现通信的方式都是基于这两种模型实现的

一、使用volatile关键字

基于 volatile 关键字来实现线程间相互通信是使用共享内存的思想,大致意思就是多个线程同时监听一个变量,当这个变量发生变化的时候 ,线程能够感知并执行相应的业务。这也是最简单的一种实现方式

public class TestSync {
	// 定义一个共享变量来实现通信,它需要是volatile修饰,否则线程不能及时感知
	public volatile static boolean listen=false;
	public static void main(String[] args) {
		List<Integer> tmp=new ArrayList<Integer>();
		Thread th1=new Thread(()->{
			for(int i=0;i<10;i++) {
				tmp.add(i);
				System.out.println("线程1向数组中添加一个元素,此时数组的长度为:"+tmp.size());
				try {
	//线程A每次执行间隔500毫秒
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if(i==5) {
					listen=true;
				}
			}
		});
		Thread th2=new Thread(()->{
			while(true) {
				if(listen) {
					System.out.println("线程1收到通知,开始执行");
					break;
				}
			}
		});
//须先启动线程2
		th2.start();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//再启动线程2
		th1.start();
	}

其执行结果为:

线程1向数组中添加一个元素,此时数组的长度为:1
线程1向数组中添加一个元素,此时数组的长度为:2
线程1向数组中添加一个元素,此时数组的长度为:3
线程1向数组中添加一个元素,此时数组的长度为:4
线程1向数组中添加一个元素,此时数组的长度为:5
线程1向数组中添加一个元素,此时数组的长度为:6
线程1向数组中添加一个元素,此时数组的长度为:7
线程1收到通知,开始执行
线程1向数组中添加一个元素,此时数组的长度为:8
线程1向数组中添加一个元素,此时数组的长度为:9
线程1向数组中添加一个元素,此时数组的长度为:10

二、使用Object类的wait() 和 notify() 方法
 众所周知,Object类提供了线程间通信的方法:wait()notify()notifyaAl(),它们是多线程通信的基础,而这种实现方式的思想自然是线程间通信。要注意的是wait和 notify必须配合synchronized使用,wait方法释放锁,notify方法不释放锁

public class TestSync {
	// 定义一个共享变量来实现通信,它需要是volatile修饰,否则线程不能及时感知
//	public volatile static boolean listen=false;
	public static void main(String[] args) {
		Object lock=new Object();
		List<Integer> tmp=new ArrayList<Integer>();
		Thread th1=new Thread(()->{
			synchronized (lock) {
				for(int i=0;i<10;i++) {
				tmp.add(i);
				System.out.println("线程1向数组中添加一个元素,此时数组的长度为:"+tmp.size());
				try {
	//线程A每次执行间隔500毫秒
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
//				if(i==5) {
//					listen=true;
//				}
				if(i==5) {
					lock.notify();//唤醒线程2
				}
			}
		 }
			
		});
		Thread th2=new Thread(()->{
			while(true) {
//				if(listen) {
//					System.out.println("线程1收到通知,开始执行");
//					break;
//				}
				synchronized (lock) {
                    if (tmp.size() != 5) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("线程B收到通知,开始执行自己的业务...");
                }

			}
		});
		th2.start();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		th1.start();
	}

}

其打印结果为:

线程1向数组中添加一个元素,此时数组的长度为:1
线程1向数组中添加一个元素,此时数组的长度为:2
线程1向数组中添加一个元素,此时数组的长度为:3
线程1向数组中添加一个元素,此时数组的长度为:4
线程1向数组中添加一个元素,此时数组的长度为:5
线程1向数组中添加一个元素,此时数组的长度为:6
线程1向数组中添加一个元素,此时数组的长度为:7
线程1向数组中添加一个元素,此时数组的长度为:8
线程1向数组中添加一个元素,此时数组的长度为:9
线程1向数组中添加一个元素,此时数组的长度为:10
线程B收到通知,开始执行自己的业务...
从这个打印结果中可以看出在线程1发出notify()唤醒线程2后,仍旧是走完了自己的线程业务,才执行线程2,这也正好说明了notify方法不释放锁,wait方法才释放锁。

三、使用JUC工具类 CountDownLatch

jdk1.5之后在java.util.concurrent包下提供了很多并发编程相关的工具类,简化了我们的并发编程代码的书写,***CountDownLatch***基于AQS框架,相当于也是维护了一个线程间共享变量state

public class TestSync {
	// 定义一个共享变量来实现通信,它需要是volatile修饰,否则线程不能及时感知
//	public volatile static boolean listen=false;
	public static void main(String[] args) {
//		Object lock=new Object();
		CountDownLatch countDownLatch = new CountDownLatch(1);
		List<Integer> tmp=new ArrayList<Integer>();
		Thread th1=new Thread(()->{
//			synchronized (lock) {
				for(int i=0;i<10;i++) {
				tmp.add(i);
				System.out.println("线程1向数组中添加一个元素,此时数组的长度为:"+tmp.size());
				try {
	//线程A每次执行间隔500毫秒
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
//				if(i==5) {
//					listen=true;
//				}
//				if(i==5) {
//					lock.notify();//唤醒线程2
//				}
				if(tmp.size()==5) {
					countDownLatch.countDown();
				}
			}
//		 }
			
		});
		Thread th2=new Thread(()->{
			while(true) {
//				if(listen) {
//					System.out.println("线程1收到通知,开始执行");
//					break;
//				}
//				synchronized (lock) {
                    if (tmp.size() != 5) {
                        try {
                        	 countDownLatch.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("线程B收到通知,开始执行自己的业务...");
                    break;
                }

//			}
		});
		th2.start();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		th1.start();
	}

}

 其执行结果为:

线程1向数组中添加一个元素,此时数组的长度为:1
线程1向数组中添加一个元素,此时数组的长度为:2
线程1向数组中添加一个元素,此时数组的长度为:3
线程1向数组中添加一个元素,此时数组的长度为:4
线程1向数组中添加一个元素,此时数组的长度为:5
线程1向数组中添加一个元素,此时数组的长度为:6
线程B收到通知,开始执行自己的业务...
线程1向数组中添加一个元素,此时数组的长度为:7
线程1向数组中添加一个元素,此时数组的长度为:8
线程1向数组中添加一个元素,此时数组的长度为:9
线程1向数组中添加一个元素,此时数组的长度为:10
 

四、使用 ReentrantLock 结合 Condition



import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class TestSync {
	// 定义一个共享变量来实现通信,它需要是volatile修饰,否则线程不能及时感知
//	public volatile static boolean listen=false;
	public static void main(String[] args) {
//		Object lock=new Object();
//		CountDownLatch countDownLatch = new CountDownLatch(1);
		
		ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
		List<Integer> tmp=new ArrayList<Integer>();
		Thread th1=new Thread(()->{
			lock.lock();
//			synchronized (lock) {
				for(int i=0;i<10;i++) {
				tmp.add(i);
				System.out.println("线程1向数组中添加一个元素,此时数组的长度为:"+tmp.size());
				try {
	//线程A每次执行间隔500毫秒
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
//				if(i==5) {
//					listen=true;
//				}
//				if(i==5) {
//					lock.notify();//唤醒线程2
//				}
//				if(tmp.size()==5) {
//					countDownLatch.countDown();
//				}
				if(tmp.size()==5) {
					condition.signal();
				}
			}
				lock.unlock();
//		 }
			
		});
		Thread th2=new Thread(()->{
//			while(true) {
//				if(listen) {
//					System.out.println("线程1收到通知,开始执行");
//					break;
//				}
//				synchronized (lock) {
			lock.lock();
                    if (tmp.size() != 5) {
                        try {
//                        	 countDownLatch.await();
                        	condition.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("线程B收到通知,开始执行自己的业务...");
//                    break;
                    lock.unlock();
//                }

//			}
		});
		th2.start();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		th1.start();
	}

}

 其执行结果为:

线程1向数组中添加一个元素,此时数组的长度为:1
线程1向数组中添加一个元素,此时数组的长度为:2
线程1向数组中添加一个元素,此时数组的长度为:3
线程1向数组中添加一个元素,此时数组的长度为:4
线程1向数组中添加一个元素,此时数组的长度为:5
线程1向数组中添加一个元素,此时数组的长度为:6
线程1向数组中添加一个元素,此时数组的长度为:7
线程1向数组中添加一个元素,此时数组的长度为:8
线程1向数组中添加一个元素,此时数组的长度为:9
线程1向数组中添加一个元素,此时数组的长度为:10
线程B收到通知,开始执行自己的业务...

显然这种方式使用起来并不是很好,代码编写复杂,而且线程2在被1唤醒之后由于没有获取锁还是不能立即执行,也就是说,A在唤醒操作之后,并不释放锁。这种方法跟 Object 的 wait() 和 notify() 一样。

五、基本LockSupport实现线程间的阻塞和唤醒

LockSupport 是一种非常灵活的实现线程间阻塞和唤醒的工具,使用它不用关注是等待线程先进行还是唤醒线程先运行,但是得知道线程的名字。



import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

public class TestSync {
	// 定义一个共享变量来实现通信,它需要是volatile修饰,否则线程不能及时感知
//	public volatile static boolean listen=false;
	public static void main(String[] args) {
//		Object lock=new Object();
//		CountDownLatch countDownLatch = new CountDownLatch(1);
		
//		ReentrantLock lock = new ReentrantLock();
//        Condition condition = lock.newCondition();
		List<Integer> tmp=new ArrayList<Integer>();
		Thread th2=new Thread(()->{
//			while(true) {
//				if(listen) {
//					System.out.println("线程1收到通知,开始执行");
//					break;
//				}
//				synchronized (lock) {
//			lock.lock();
                    if (tmp.size() != 5) {
                    	LockSupport.park();
//                        try {
//                        	 countDownLatch.await();
//                        	condition.await();
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
                    }
                    System.out.println("线程B收到通知,开始执行自己的业务...");
//                    break;
//                    lock.unlock();
//                }

//			}
		});
		Thread th1=new Thread(()->{
//			lock.lock();
//			synchronized (lock) {
				for(int i=0;i<10;i++) {
				tmp.add(i);
				System.out.println("线程1向数组中添加一个元素,此时数组的长度为:"+tmp.size());
				try {
	//线程A每次执行间隔500毫秒
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
//				if(i==5) {
//					listen=true;
//				}
//				if(i==5) {
//					lock.notify();//唤醒线程2
//				}
//				if(tmp.size()==5) {
//					countDownLatch.countDown();
//				}
//				if(tmp.size()==5) {
//					condition.signal();
//				}
				if(tmp.size()==5) {
					LockSupport.unpark(th2);
				}
			}
//				lock.unlock();
//		 }
			
		});
		
		th2.start();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		th1.start();
	}

}

其执行结果为:

线程1向数组中添加一个元素,此时数组的长度为:1
线程1向数组中添加一个元素,此时数组的长度为:2
线程1向数组中添加一个元素,此时数组的长度为:3
线程1向数组中添加一个元素,此时数组的长度为:4
线程1向数组中添加一个元素,此时数组的长度为:5
线程1向数组中添加一个元素,此时数组的长度为:6
线程B收到通知,开始执行自己的业务...
线程1向数组中添加一个元素,此时数组的长度为:7
线程1向数组中添加一个元素,此时数组的长度为:8
线程1向数组中添加一个元素,此时数组的长度为:9
线程1向数组中添加一个元素,此时数组的长度为:10 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值