多线程顺序问题

小王小王全是脂肪 2019-08-11 07:23:47
写三个线程abc同时启动,要求bc都执行完后a才执行完毕
...全文
140 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
=PNZ=BeijingL 2019-09-12
  • 打赏
  • 举报
回复
也可以使用lock锁, ABCABCABC这样的顺序执行


public class ConditionSortProcess {

volatile public static int PRINT_NO = 1;
private static ReentrantLock lock = new ReentrantLock();
final private static Condition conditionA = lock.newCondition();
final private static Condition conditionB = lock.newCondition();
final private static Condition conditionC = lock.newCondition();

public static void main(String[] args) {
Thread thread = new Thread() {
@Override
public void run() {
try {
lock.lock();
while (PRINT_NO != 1) {
conditionA.await();
}

System.out.println("Thread A ");
PRINT_NO = 2;
conditionB.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
};

Thread threadB = new Thread() {
@Override
public void run() {
try {
lock.lock();
while (PRINT_NO != 2) {
conditionA.await();
}

System.out.println("Thread B ");
PRINT_NO = 3;
conditionC.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
};

Thread threadC = new Thread() {
@Override
public void run() {
try {
lock.lock();
while (PRINT_NO != 3) {
conditionA.await();
}

System.out.println("Thread C ");
PRINT_NO = 1;
conditionA.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
};

Thread[] array1 = new Thread[5];
Thread[] array2 = new Thread[5];
Thread[] array3 = new Thread[5];
for (int i = 0; i < 5; i++) {
array1[i] = new Thread(thread);
array2[i] = new Thread(threadB);
array3[i] = new Thread(threadC);

array1[i].start();
array2[i].start();
array3[i].start();
}
}


}
guishuanglin 2019-08-12
  • 打赏
  • 举报
回复
楼上没有问题, 当然还可以用其它简单办法实现: 用方法回调就可以了.
faith.huan 2019-08-11
  • 打赏
  • 举报
回复
实现方式很多,提供个简单的例子


public class ThreadOrderControlTest {


    public static void main(String[] args) {

        CountDownLatch countDownLatch = new CountDownLatch(2);

        new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程" + Thread.currentThread().getName() + "结束");
            countDownLatch.countDown();
        }, "a").start();

        new Thread(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程" + Thread.currentThread().getName() + "结束");
            countDownLatch.countDown();
        }, "b").start();

        new Thread(() -> {
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程" + Thread.currentThread().getName() + "结束");
        }, "c").start();
    }
}

51,412

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧