当前位置:   article > 正文

场景题:有三个线程t1、t2、t3。确保三个线程t1执行完后t2执行,t2执行完成后t3执行_编写程序task51.c,主线程创建3个对等线程t1、t2、t3,每个线程利用循环执行5次prin

编写程序task51.c,主线程创建3个对等线程t1、t2、t3,每个线程利用循环执行5次prin

场景题:有三个线程t1、t2、t3。确保三个线程t1执行完后t2执行,t2执行完成后t3执行

主要有两种方法:

  1. 通过在当前线程传入上一线程对象,调用join()方法让当前线程阻塞等待上一线程执行完毕的方式,实现代码如下:
public class Main{

	public static void main(String[] args) {
		Thread t1 = new Thread(new Task(null));
        Thread t2 = new Thread(new Task(t1));
        Thread t3 = new Thread(new Task(t2));
        t1.start();
        t2.start();
        t3.start();
	}
	
	static class Task implements Runnable {
        private Thread preThread;
        public Task(Thread preThread) {
            this.preThread = preThread;
        }
        public void run() {
            if (preThread != null) {
                try {
                    preThread.join();
                    System.out.println("thread start:" + Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("thread start:" + Thread.currentThread().getName());
            }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  1. 通过CountDownLatch计数器去实现,初始化上一线程与当前线程的计数器,通过await()方法阻塞等待,countDown()方法减计数器的值,当计数器的值减到0时当前线程开始运行。实现代码如下:
public class Main02{
	public static void main(String[] args) {
		CountDownLatch countDownLatch1 = new CountDownLatch(0);// 在这里count为0,表示该线程立马开始执行
        CountDownLatch countDownLatch2 = new CountDownLatch(1);// 在这里count为1,表示等待上一线程执行完,也就是要等countDownLatch2减去1为0时
        CountDownLatch countDownLatch3 = new CountDownLatch(1);// 在这里count为1,表示等待上一线程执行完
        Thread t1 = new Thread(new Count(countDownLatch1,countDownLatch2)); 
        Thread t2 = new Thread(new Count(countDownLatch2, countDownLatch3));
        Thread t3 = new Thread(new Count(countDownLatch3, countDownLatch3));
	}
	static class Count implements Runnable {

        CountDownLatch countDownLatch1;
        CountDownLatch countDownLatch2;

        /**
         * @param countDownLatch1 上一个线程的计数器
         * @param countDownLatch2 当前线程的计数器
         */
        public Count(CountDownLatch countDownLatch1, CountDownLatch countDownLatch2) {
            super();
            this.countDownLatch1 = countDownLatch1;
            this.countDownLatch2 = countDownLatch2;
        }

        @Override
        public void run() {
            try {
                countDownLatch1.await();//线程t1进来的时候,因为count=0,所以不用等直接运行
                System.out.println("thread start:" + Thread.currentThread().getName());
                Thread.sleep(1000);
                countDownLatch2.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
	
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/804970
推荐阅读
相关标签
  

闽ICP备14008679号