关于线程任务执行排序

关于线程任务排序问题的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();
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值