Java多线程实战篇:三个线程分别打印A,B,C,要求这三个线程一起运行,打印n次,输出形如“ABCABCABC

1、使用Lock

public class PrintABCUsingLock {
	// 打印次数
	private int times;
	private int state;
	private Lock lock = new ReentrantLock();

	public PrintABCUsingLock(int times) {
		this.times = times;
	}

	public static void main(String[] args) {
		PrintABCUsingLock printABCUsingLock = new PrintABCUsingLock(10);
		new Thread(() -> {
			printABCUsingLock.printA();
		}).start();
		new Thread(() -> {
			printABCUsingLock.printB();
		}).start();
		new Thread(() -> {
			printABCUsingLock.printC();
		}).start();
	}

	public void printA() {
		print("A", 0);
	}

	public void printB() {
		print("B", 1);
	}

	public void printC() {
		print("C", 2);
	}

	// 假如执行printA的线程进来,state变为1,第二个进来的线程不一定是执行printB的线程,如果不是不能进入if打印name,直到执行printB的线程进来,state变为2,如此循环下去
	private void print(String name, int targetState) {
		for (int i = 0; i < times;) {
			lock.lock();
			if (state % 3 == targetState) {
				state++;
				i++;
				System.out.print(name);
			}
			lock.unlock();
		}
	}
}

2、使用Semaphore

public class PrintABCUsingSemaphore {
	// 打印次数
	private int times;
	Semaphore semaphoreA = new Semaphore(1);
	Semaphore semaphoreB = new Semaphore(0);
	Semaphore semaphoreC = new Semaphore(0);

	public PrintABCUsingSemaphore(int times) {
		this.times = times;
	}

	public void printA() {
		try {
			print("A", semaphoreA, semaphoreB);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void printB() {
		try {
			print("B", semaphoreB, semaphoreC);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void printC() {
		try {
			print("C", semaphoreC, semaphoreA);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void print(String str, Semaphore current, Semaphore next) throws InterruptedException {
		for (int i = 0; i < times; ++i) {
			current.acquire();
			System.out.print(str);
			next.release();
		}
	}

	public static void main(String[] args) {
		PrintABCUsingSemaphore printABCUsingSemaphore = new PrintABCUsingSemaphore(10);
		new Thread(() -> {
			printABCUsingSemaphore.printA();
		}).start();
		new Thread(() -> {
			printABCUsingSemaphore.printB();
		}).start();
		new Thread(() -> {
			printABCUsingSemaphore.printC();
		}).start();
	}
}

3、使用wait/notify

public class PrintABCUsingWaitNotify {
	private int times;
	private int state;
	private Object objectA = new Object();
	private Object objectB = new Object();
	private Object objectC = new Object();

	public PrintABCUsingWaitNotify(int times) {
		this.times = times;
	}

	public static void main(String[] args) {
		PrintABCUsingWaitNotify printABCUsingWaitNotify = new PrintABCUsingWaitNotify(10);
		new Thread(() -> {
			printABCUsingWaitNotify.printA();
		}).start();
		new Thread(() -> {
			printABCUsingWaitNotify.printB();
		}).start();
		new Thread(() -> {
			printABCUsingWaitNotify.printC();
		}).start();
	}

	public void printA() {
		try {
			print("A", 0, objectA, objectB);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void printB() {
		try {
			print("B", 1, objectB, objectC);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void printC() {
		try {
			print("C", 2, objectC, objectA);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private void print(String name, int targetState, Object current, Object next) throws InterruptedException {
		for (int i = 0; i < times;) {
			synchronized (current) {
				while (state % 3 != targetState) {
					current.wait();
				}
				state++;
				i++;
				System.out.print(name);
				synchronized (next) {
					next.notify();
				}
			}
		}
	}
}

4、使用Lock/Condition

public class PrintABCUsingLockCondition {
	private int times;
	private int state;
	private Lock lock = new ReentrantLock();
	private Condition conditionA = lock.newCondition();
	private Condition conditionB = lock.newCondition();
	private Condition conditionC = lock.newCondition();

	public PrintABCUsingLockCondition(int times) {
		this.times = times;
	}

	public static void main(String[] args) {
		PrintABCUsingLockCondition printABCUsingLockCondition = new PrintABCUsingLockCondition(10);
		new Thread(() -> {
			printABCUsingLockCondition.printA();
		}).start();
		new Thread(() -> {
			printABCUsingLockCondition.printB();
		}).start();
		new Thread(() -> {
			printABCUsingLockCondition.printC();
		}).start();
	}

	public void printA() {
		print("A", 0, conditionA, conditionB);
	}

	public void printB() {
		print("B", 1, conditionB, conditionC);
	}

	public void printC() {
		print("C", 2, conditionC, conditionA);
	}

	private void print(String name, int targetState, Condition current, Condition next) {
		for (int i = 0; i < times;) {
			lock.lock();
			try {
				while (state % 3 != targetState) {
					current.await();
				}
				state++;
				i++;
				System.out.print(name);
				next.signal();
			} catch (InterruptedException e) {
				e.printStackTrace();
			} finally {
				lock.unlock();
			}
		}
	}
}
  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邋遢的流浪剑客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值