3种方式实现3个线程打印递增的数字

该博客探讨了三种实现多线程同步的方法:使用synchronized的wait/notifyAll,Condition接口,以及LockSupport工具类。通过创建三个线程交替打印递增数字,展示了每种方法的代码实现和运行结果,强调了在并发控制中的不同策略和应用场景。
摘要由CSDN通过智能技术生成

问题描述:启动3个线程打印递增的数字, 线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 然后是线程3打印11,12,13,14,15.接着再由线程1打印16,17,18,19,20….以此类推, 直到打印到75。

方式一:synchronized结合wait/notifyAll实现

public class ThreeThread {

	public static void main(String[] args) {
		Object o = new Object();
		new Thread(new PrintRunnable(o, 1)).start();
		new Thread(new PrintRunnable(o, 2)).start();
		new Thread(new PrintRunnable(o, 3)).start();
	}
}

class PrintRunnable implements Runnable {
	private Object o;
	private int threadId;
	private static int num = 1;

	public PrintRunnable(Object o, int threadId) {
		this.o = o;
		this.threadId = threadId;
	}

	@Override
	public void run() {
		while (num < 65) {//65是因为刚开始3个线程拿到num都为1
			synchronized (o) {
				while (num / 5 % 3 + 1 != threadId) {
					try {
						o.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				System.out.print("线程:" + threadId + ": ");
				for (int i = 5; i > 0; i--, num++) {
					System.out.print(" " + num);
				}
				System.out.println();
				o.notifyAll();
			}
		}

	}
}

运行结果:

线程:1:  1 2 3 4 5
线程:2:  6 7 8 9 10
线程:3:  11 12 13 14 15
线程:1:  16 17 18 19 20
线程:2:  21 22 23 24 25
线程:3:  26 27 28 29 30
线程:1:  31 32 33 34 35
线程:2:  36 37 38 39 40
线程:3:  41 42 43 44 45
线程:1:  46 47 48 49 50
线程:2:  51 52 53 54 55
线程:3:  56 57 58 59 60
线程:1:  61 62 63 64 65
线程:2:  66 67 68 69 70
线程:3:  71 72 73 74 75

方式二:Condition接口方式实现

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

public class ThreeThreadWithCondition {

	static Lock lock = new ReentrantLock();
	static Condition A = lock.newCondition();
	static Condition B = lock.newCondition();
	static Condition C = lock.newCondition();
	static int num = 1;
	static int count = 0;

	static class PrintRunnableA implements Runnable {

		@Override
		public void run() {
			try {
				lock.lock();
				while (num < 65) {
					while (count % 3 != 0) {
						A.await();
					}
					System.out.print("线程:" + Thread.currentThread().getName() + ": ");
					for (int i = 5; i > 0; i--, num++) {
						System.out.print(" " + num);
					}
					System.out.println();
					count++;
					B.signal();
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			} finally {
				lock.unlock();
			}

		}

	}

	static class PrintRunnableB implements Runnable {

		@Override
		public void run() {
			try {
				lock.lock();
				while (num < 65) {
					while (count % 3 != 1) {
						B.await();
					}
					System.out.print("线程:" + Thread.currentThread().getName() + ": ");
					for (int i = 5; i > 0; i--, num++) {
						System.out.print(" " + num);
					}
					System.out.println();
					count++;
					C.signal();
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			} finally {
				lock.unlock();
			}

		}

	}

	static class PrintRunnableC implements Runnable {

		@Override
		public void run() {
			try {
				lock.lock();
				while (num < 65) {
					while (count % 3 != 2) {
						C.await();
					}
					System.out.print("线程:" + Thread.currentThread().getName() + ": ");
					for (int i = 5; i > 0; i--, num++) {
						System.out.print(" " + num);
					}
					System.out.println();
					count = 0;
					A.signal();
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			} finally {
				lock.unlock();
			}

		}

	}

	public static void main(String[] args) {
		new Thread(new PrintRunnableA()).start();
		new Thread(new PrintRunnableB()).start();
		new Thread(new PrintRunnableC()).start();

	}

}

运行结果:

线程:Thread-0:  1 2 3 4 5
线程:Thread-1:  6 7 8 9 10
线程:Thread-2:  11 12 13 14 15
线程:Thread-0:  16 17 18 19 20
线程:Thread-1:  21 22 23 24 25
线程:Thread-2:  26 27 28 29 30
线程:Thread-0:  31 32 33 34 35
线程:Thread-1:  36 37 38 39 40
线程:Thread-2:  41 42 43 44 45
线程:Thread-0:  46 47 48 49 50
线程:Thread-1:  51 52 53 54 55
线程:Thread-2:  56 57 58 59 60
线程:Thread-0:  61 62 63 64 65
线程:Thread-1:  66 67 68 69 70
线程:Thread-2:  71 72 73 74 75

方式三:LockSupport方式实现

import java.util.concurrent.locks.LockSupport;

public class ThreeThreadLockSupport {

	static int num = 1;
	static int count = 0;
	Thread t1 = new Thread();
	Thread t2 = new Thread();
	Thread t3 = new Thread();

	public void doPrint() {

		t1 = new Thread(new Runnable() {

			@Override
			public void run() {
				while (num < 65) {
					while (count % 3 != 0) {
						LockSupport.park();
					}
					System.out.print("线程:" + Thread.currentThread().getName() + ": ");
					for (int i = 5; i > 0; i--, num++) {
						System.out.print(" " + num);
					}
					System.out.println();
					count++;
					LockSupport.unpark(t2);
				}

			}
		}, "t1");
		t1.start();

		t2 = new Thread(new Runnable() {

			@Override
			public void run() {
				while (num < 65) {
					while (count % 3 != 1) {
						LockSupport.park();
					}
					System.out.print("线程:" + Thread.currentThread().getName() + ": ");
					for (int i = 5; i > 0; i--, num++) {
						System.out.print(" " + num);
					}
					System.out.println();
					count++;
					LockSupport.unpark(t3);
				}

			}
		}, "t2");
		t2.start();
		
		t3 = new Thread(new Runnable() {

			@Override
			public void run() {
				while (num < 65) {
					while (count % 3 != 2) {
						LockSupport.park();
					}
					System.out.print("线程:" + Thread.currentThread().getName() + ": ");
					for (int i = 5; i > 0; i--, num++) {
						System.out.print(" " + num);
					}
					System.out.println();
					count = 0;
					LockSupport.unpark(t1);
				}

			}
		}, "t3");
		t3.start();
	}

	public static void main(String[] args) {
		ThreeThreadLockSupport lockSupport = new ThreeThreadLockSupport();
		lockSupport.doPrint();
	}
}

运行结果:

线程:t1:  1 2 3 4 5
线程:t2:  6 7 8 9 10
线程:t3:  11 12 13 14 15
线程:t1:  16 17 18 19 20
线程:t2:  21 22 23 24 25
线程:t3:  26 27 28 29 30
线程:t1:  31 32 33 34 35
线程:t2:  36 37 38 39 40
线程:t3:  41 42 43 44 45
线程:t1:  46 47 48 49 50
线程:t2:  51 52 53 54 55
线程:t3:  56 57 58 59 60
线程:t1:  61 62 63 64 65
线程:t2:  66 67 68 69 70
线程:t3:  71 72 73 74 75

今天突然接收到基友抛出的问题,花了点时间总结了下实现方法。

学无止境,每天进步一点点 ~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值