关于线程任务排序问题的4种解决方案
例如:有线程A、B、C三个线程,请按照A、B、C执行打印语句
方式一、利用线程池实现
/**
* @Description: 线程池队列形式实现,先进先出原则
* @Package: com.cpo
* @ClassName: ThreadExcutorScort
* @Author: cp
* @Date: 2020/5/18 11:11
* @Version: 1.0
*/
public class ThreadExcutorScort {
public static void main(String[] args){
Thread thread1=new Thread(()-> System.out.println("A执行了"));
Thread thread2=new Thread(()-> System.out.println("B执行了"));
Thread thread3=new Thread(()-> System.out.println("C执行了"));
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(thread1);
executorService.execute(thread2);
executorService.execute(thread3);
executorService.shutdown();
}
}
方式二、线程方法JOIN实现
/**
* @Description: 指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程
* @Package: com.cpo
* @ClassName: ThreadJoinScort
* @Author: cp
* @Date: 2020/5/18 10:31
* @Version: 1.0
*/
public class ThreadJoinScort {
public static void main(String[] args) throws Exception{
ThreadJoin1 join1=new ThreadJoin1();
Thread thread1=new Thread(join1,"A");
thread1.start();
ThreadJoin2 join2=new ThreadJoin2();
Thread thread2=new Thread(join2,"B");
thread1.join();
thread2.start();
ThreadJoin3 join3=new ThreadJoin3();
Thread thread3=new Thread(join3,"C");
thread2.join();
thread3.start();
}
}
class ThreadJoin1 implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"执行了!!!!");
}
}
class ThreadJoin2 implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"执行了!!!!");
}
}
class ThreadJoin3 implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"执行了!!!!");
}
}
方式三、信号标记法
方法一:通过synchronzied关键字
/**
* @Package: com.cpo
* @ClassName: ThreadFlagScort
* @Author: cp
* @Date: 2020/5/18 11:21
* @Version: 1.0
*/
public class ThreadFlagScort {
public static void main(String[] args){
ThreadFlagScort flagScort=new ThreadFlagScort();
new Thread(()->{
try {
flagScort.printA();
}catch (InterruptedException e){
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
flagScort.printB();
}catch (InterruptedException e){
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
flagScort.printC();
}catch (InterruptedException e){
e.printStackTrace();
}
}).start();
}
private int flag=1;
private void printA() throws InterruptedException{
synchronized (this) {
if (flag != 1) {
this.wait();
}
System.out.println("A执行了");
flag=2;
this.notifyAll();
}
}
private synchronized void printB() throws InterruptedException{
if (flag != 2) {
this.wait();
}
System.out.println("B执行了");
flag=3;
this.notifyAll();
}
private synchronized void printC() throws InterruptedException{
if (flag != 3) {
this.wait();
}
System.out.println("C执行了");
flag=1;
this.notifyAll();
}
}
方法二、通过Lock类实现
/**
* @Package: com.cpo
* @ClassName: ThreadLockScort
* @Author: cp
* @Date: 2020/5/18 11:37
* @Version: 1.0
*/
public class ThreadLockScort {
public static void main(String[] args) {
ThreadLockScort lockScort=new ThreadLockScort();
new Thread(()->{
lockScort.printA();
}).start();
new Thread(()->{
lockScort.printB();
}).start();
new Thread(()->{
lockScort.printC();
}).start();
}
private ReentrantLock lock=new ReentrantLock();
private Condition ca = lock.newCondition();
private Condition cb = lock.newCondition();
private Condition cc = lock.newCondition();
private int flag=1;
private void printA(){
lock.lock();
try {
while (flag!=1){
ca.await();
}
System.out.println("A执行了");
flag=2;
cb.signal();
}catch (InterruptedException e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
private void printB(){
lock.lock();
try {
while (flag!=2){
cb.await();
}
System.out.println("B执行了");
flag=3;
cc.signal();
}catch (InterruptedException e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
private void printC(){
lock.lock();
try {
while (flag!=3){
cc.await();
}
System.out.println("C执行了");
flag=1;
ca.signal();
}catch (InterruptedException e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
}