如何让多个线程按照顺序执行

两种方法:

假设如下:

                如果有a,b,c三个线程,我们依次执行这三个线程。因为不保证有序性,可能执行出来是混乱的

解决如下:

第一种使用join()方法,这个方法的作用是,让当前执行线程等待直到调用join方法的线程结束运行

代码如下:

class Thread1 extends Thread{
    public void run(){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Threada");
    }
}


class Thread2 extends Thread{
    public void run(){
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Threadb");
    }
}


class Thread3 extends Thread{
    public void run(){
        System.out.println("Threadc");
    }
}

public class Main {
    public static void main(String[] args) throws InterruptedException {
           Thread1 thread1=new Thread1();
               thread1.start();
               thread1.join();
           Thread2 thread2=new Thread2();
               thread2.start();
               thread2.join();
           Thread3 thread3=new Thread3();
           thread3.start();
//        ExecutorService executorService=Executors.newSingleThreadExecutor();
//        executorService.execute(thread1);
//        executorService.execute(thread2);
//        executorService.execute(thread3);
//        executorService.shutdown();
    }
}

第二种是通过线程池里的方法:我们使用单线程池,这样就能根据传入的顺序执行线程。

package didi;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class Thread1 extends Thread{
    public void run(){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Threada");
    }
}


class Thread2 extends Thread{
    public void run(){
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Threadb");
    }
}


class Thread3 extends Thread{
    public void run(){
        System.out.println("Threadc");
    }
}

public class Main {
    public static void main(String[] args) throws InterruptedException {
           Thread1 thread1=new Thread1();
//               thread1.start();
//               thread1.join();
          Thread2 thread2=new Thread2();
//               thread2.start();
//               thread2.join();
          Thread3 thread3=new Thread3();
//           thread3.start();
        ExecutorService executorService=Executors.newSingleThreadExecutor();
        executorService.execute(thread1);
        executorService.execute(thread2);
        executorService.execute(thread3);
        executorService.shutdown();
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值