编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

bergerx 线程 2022-5-13 10:34 33人围观

腾讯云服务器
如何确保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线程按特定顺序运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^