如何确保Java线程按特定顺序运行 [英] How to ensure Java threads run in a particular order

查看:55
本文介绍了如何确保Java线程按特定顺序运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出三个打印字母A-C的线程1-3,如何保证输出顺序?

Given three threads, 1-3, that print a letter, A-C, how can I guarantee the output order?

我希望线程的输出为"ABCABCABC"

I would like the output of the threads to be "ABCABCABC"

推荐答案

线程是独立运行的,因此,除非您做出特殊的努力来同步线程,否则您将永远不会得到这样的输出.预计3个独立运行的线程将打印随机"输出,因为要由OS来调度线程.

Threads run independently, so you will never get such output unless you perform special efforts to synchronize your threads. 3 threads that are running independently are expected to print "random" output because it is up to OS to schedule the threads.

这篇关于如何确保Java线程按特定顺序运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆