顺序执行线程A、B、C

专题文章:
  JAVA并发编程

1、synchronized+wait/notify

public class WaitNotifyABC{
	private int num = 0;
	private static final Object LOCK= new Object();
	private void printABC(String name,int targeNum){
		synchronized(LOCK){
			while(num % 3 != targeNum){
				try{
					LOCK.wait();
				}catch(InterruptedException e){
					e.printStackTrace();
				}
			}
			++num;
			System.out.print(name);
			LOCK.notifyAll();
		}
	}
	public static void main(String[] args){
		WaitNotifyABC waitNotifyABC = new WaitNotifyABC();
		new Thread(() -> {
			waitNotifyABC.printABC("A",0);},"A").start();
		new Thread(() -> {
			waitNotifyABC.printABC("B",1);},"A").start();
		new Thread(() -> {
			waitNotifyABC.printABC("C",2);},"A").start();
	}
}

2、join

public class PrintABC implements Runnable{
	private Thread beforeThread;
	public PrintABC(Thread beforeThread){
		this.beforeThread = beforeThread;
	}

	@Override
	public void run(){
		if(beforeThread != null){
			try{
				beforeThread.join();
				System.out.print(Thread.currentThread().getName());
			}catch(Exception e){
				e.printStackTrace();
			}
		}else{
			System.out.print(Thread.currentThread().getName());
		}
	}

	public static void main(String[] args){
		Thread t0 = new Thread(new PrintABC(null),"A");
		Thread t1 = new Thread(new PrintABC(t0),"B");  // 要开始t1的前提是t0要结束
		Thread t2 = new Thread(new PrintABC(t1),"C");
		t0.start(); // 会去调用run()
		t1.start();
		t2.start();
	}
}

3、LOCK

import java.util.concurrent.locks.*;
public class LockABC{
	private int num = 0;
	private Lock lock = new ReentrantLock();
	
	public void printABC(String name,int targetNum){
		lock.lock();
		if(num % 4 == targetNum){
			++num;
			System.out.println(Thread.currentThread().getName() + " " + name);
		}
		lock.unlock();
	}
	
	public static void main(String[] args){
		LockABC lockABC = new LockABC();
		new Thread(() -> {
			lockABC.printABC("A",0);
		},"Thread-A").start();

		new Thread(() -> {
			lockABC.printABC("B",1);
		},"Thread-B").start();
		
		// 运行结果中该线程没启动,没想通
		new Thread(() -> {
			lockABC.printABC("C",2);
		},"Thread-C").start();

	}
}

Thread-C线程没启动,没想通,知道的告诉我下

4、LOCK + Condition 对 3 的优化

import java.util.concurrent.locks.*;
public class LockConditionABC{
	private int num;
	private static Lock lock = new ReentrantLock();
	private static Condition c1 = lock.newCondition();
	private static Condition c2 = lock.newCondition();
	private static Condition c3 = lock.newCondition();

	private void printABC(String name,int targetNum,Condition currentThread,Condition nextThread){
		lock.lock();
		try{
			while(num % 3 != targetNum){
				currentThread.await();
			}
			num++;
			System.out.println(Thread.currentThread().getName());
			nextThread.signal();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			lock.unlock();
		}
	}
	public static void main(String[] args){
		LockConditionABC lockConditionABC = new LockConditionABC();
		new Thread(() -> {
			lockConditionABC.printABC("Thread-A",0,c1,c2);
		},"Thread-A").start();

		new Thread(() -> {
			 lockConditionABC .printABC("Thread-B",1,c2,c3);
		},"Thread-B").start();
		
		
		new Thread(() -> {
			 lockConditionABC .printABC("Thread-C",2,c3,c1);
		},"Thread-C").start();
	}
}

5、Semaphore

import java.util.concurrent.Semaphore;
public class SemaphoreABC{
	private static Semaphore s1 = new Semaphore(1);
	private static Semaphore s2 = new Semaphore(0);
	private static Semaphore s3 = new Semaphore(0);

	private void printABC(String name,Semaphore currentThread,Semaphore nextThread){
		try{
			currentThread.acquire();
			System.out.println(Thread.currentThread().getName());
			nextThread.release();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public static void main(String[] args){
		SemaphoreABC semaphoreABC = new SemaphoreABC();
		new Thread(() -> {
			semaphoreABC.printABC("Thread-A",s1,s2);
		},"Thread-A").start();

		new Thread(() -> {
			semaphoreABC .printABC("Thread-B",s2,s3);
		},"Thread-B").start();
		
		
		new Thread(() -> {
			semaphoreABC .printABC("Thread-C",s3,s1);
		},"Thread-C").start();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值