ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

线程间通信Demo

2021-11-06 11:30:40  阅读:107  来源: 互联网

标签:try Thread Demo number 间通信 线程 lock new condition


1、实验要求

        使用四个线程AA,BB,CC,DD,AA,CC一个对象进行加1操作,BB,DD对一个对象进行减1操作。

2、实验步骤

        (1)创建资源类

          以number为依据做加减操作判断,分别在资源类中添加加减方法使用ReentrantLock对象进行上锁和解锁。 

        其中:

                lock.lock(); // 代表上锁

                lock.unlock(); // 代表解锁

                condition.await(); // 类似于Object.wait(); 使当前线程等待,并释放锁。

                condition.signalAll(); // 类似于Object.notifyAll(); // 唤醒所有等待中的锁

         PS:

                wait();操作必须要放在while循环中判断,如果使用if来做判断则会发生虚假唤醒状况。

//创建资源类
class Share {
	// 初始值
	private int number = 0;

	// 可重入锁
	private final Lock lock = new ReentrantLock();
	private Condition condition = lock.newCondition();

	// 加1
	public void incr() throws InterruptedException {
		// 上锁
		lock.lock();
		try {

			// 判断
			while (number != 0) { // 如果不为0就等待
				condition.await();
			}
			number++;
			System.out.println(Thread.currentThread().getName() + "::" + number);
			// 通知
			condition.signalAll();
		} finally {
			lock.unlock();
		}
	}

	// 减1
	public void decr() throws InterruptedException {
		// 上锁
		lock.lock();
		try {

			// 判断
			while (number != 1) { // 如果不为1就等待
				condition.await();
			}
			number--;
			System.out.println(Thread.currentThread().getName() + "::" + number);
			// 通知
			condition.signalAll();
		} finally {
			lock.unlock();
		}
	}
}

        (2)测试类

        此处使用lambda表达式的方式创建线程并启动。

public class ThreadDemo2 {
	
	public static void main(String[] args) {
		Share share = new Share();
		
		new Thread(()->{
			for (int i = 0; i < 10; i++) {
				try {
					share.incr();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "AA").start();
		
		new Thread(()->{
			for (int i = 0; i < 10; i++) {
				try {
					share.decr();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "BB").start();
		
		new Thread(()->{
			for (int i = 0; i < 10; i++) {
				try {
					share.incr();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "CC").start();
		
		new Thread(()->{
			for (int i = 0; i < 10; i++) {
				try {
					share.decr();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "DD").start();
		
	}

}

        (3)测试

 3、整体代码

package lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadDemo2 {
	
	public static void main(String[] args) {
		Share share = new Share();
		
		new Thread(()->{
			for (int i = 0; i < 10; i++) {
				try {
					share.incr();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "AA").start();
		
		new Thread(()->{
			for (int i = 0; i < 10; i++) {
				try {
					share.decr();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "BB").start();
		
		new Thread(()->{
			for (int i = 0; i < 10; i++) {
				try {
					share.incr();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "CC").start();
		
		new Thread(()->{
			for (int i = 0; i < 10; i++) {
				try {
					share.decr();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "DD").start();
		
	}

}

//创建资源类
class Share {
	// 初始值
	private int number = 0;

	// 可重入锁
	private final Lock lock = new ReentrantLock();
	private Condition condition = lock.newCondition();

	// 加1
	public void incr() throws InterruptedException {
		// 上锁
		lock.lock();
		try {

			// 判断
			while (number != 0) { // 如果不为0就等待
				condition.await();
			}
			number++;
			System.out.println(Thread.currentThread().getName() + "::" + number);
			// 通知
			condition.signalAll();
		} finally {
			lock.unlock();
		}
	}

	// 减1
	public void decr() throws InterruptedException {
		// 上锁
		lock.lock();
		try {

			// 判断
			while (number != 1) { // 如果不为1就等待
				condition.await();
			}
			number--;
			System.out.println(Thread.currentThread().getName() + "::" + number);
			// 通知
			condition.signalAll();
		} finally {
			lock.unlock();
		}
	}
}

标签:try,Thread,Demo,number,间通信,线程,lock,new,condition
来源: https://blog.csdn.net/qq_54658577/article/details/121176219

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有