Java-控制线程顺序执行

第一种方式:

使用Thread类方法join,这个方法会一直阻塞线程,直到此线程执行结束。

package com.test.thread;

public class Test1 {
    public static void main(String[] args) throws InterruptedException {
        TestThread t1 = new TestThread("thread-1");
        TestThread t2 = new TestThread("thread-2");
        TestThread t3 = new TestThread("thread-3");
        t1.start();
        t1.join();
        t2.start();
        t2.join();
        t3.start();
    }
}

class TestThread extends Thread {
    public TestThread(String threadName) {
        super.setName(threadName);
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}

控制台输出:

thread-1
thread-2
thread-3

第二种方式:

给一个target,每个线程分别判断target是否是当前线程,如果是就执行,如果不是继续等待。

package com.test.thread;

public class Test2 {

    public static void main(String[] args) {
        new TestThread2("thread-1").start();
        new TestThread2("thread-2").start();
        new TestThread2("thread-3").start();
    }
}

class TestThread2 extends Thread {

    public static volatile String current_thread = "thread-1";//第一个要执行的线程

    public TestThread2(String threadName) {
        super.setName(threadName);
    }

    @Override
    public void run() {
        String threadName = Thread.currentThread().getName();
        while (true) {
            if (threadName.equals(current_thread)) {// 如果要执行的线程是当前线程就break,否则继续while
                break;
            }
        }
        System.out.println(threadName);
        if (threadName.equals("thread-1"))
            current_thread = "thread-2";// 下一个要执行的线程名
        else if (threadName.equals("thread-2"))
            current_thread = "thread-3";// 下一个要执行的线程名
    }
}

控制台输出:

thread-1
thread-2
thread-3

 第三种方式:

使用java.util.concurrent包下的CountDownLatch类,这个类有一个计数器,我们就可以根据这个计数器进行线程控制。

package com.test.thread;

import java.util.concurrent.CountDownLatch;

public class Test3 {
    public static void main(String[] args) throws Exception {
        CountDownLatch cdl1 = new CountDownLatch(1);
        CountDownLatch cdl2 = new CountDownLatch(1);
        CountDownLatch cdl3 = new CountDownLatch(1);

        new TestThread3(cdl1, cdl2, "thread-1").start();
        new TestThread3(cdl2, cdl3, "thread-2").start();
        new TestThread3(cdl3, null, "thread-3").start();
        
        cdl1.countDown();//将thread-1的计数变为0,此时thread会继续执行下去,
                         //其他的线程计数仍为1,所以继续阻塞在latchSelf.await();
    }
}

class TestThread3 extends Thread {
    private CountDownLatch latchSelf;// 自己的计数器
    private CountDownLatch latchNext;// 下一个线程的计数器

    public TestThread3(CountDownLatch latchSelf, CountDownLatch latchNext, String threadName) {
        this.latchSelf = latchSelf;
        this.latchNext = latchNext;
        super.setName(threadName);
    }

    @Override
    public void run() {
        try {
            latchSelf.await();// 自己的计数器为0的时候就会继续往下执行
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName());

        if (latchNext != null)
            latchNext.countDown();// 让下一个线程的计数器为0
    }
}

控制台输出:
 

thread-1
thread-2
thread-3

总结:

第一种方式使用的是Thread类提供的join方法,此种方式实际上是单线程顺序执行。第二种方式是使用了一个全局变量,线程执行的顺序通过此变量控制,此方式是并发执行,通过while进行线程等待。第三种方式使用的是java并发包的工具类,此方式是并发执行,代码可读性高,代码可控性高,代码质量高,推荐使用此方式。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值