给出三个打印字母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)!