我正在用java编写一个程序,其中必须有一些从主线程创建并在其旁边运行的线程(例如3个)。程序一步一步进行。在每个步骤中,主线程首先计算一个时钟并打印它(独占执行),然后其他线程都必须检查一些东西(它们的顺序不重要,它们必须同时工作),看看它们是否必须在这个时钟中打印出一些东西与否,这会重复。
如何以简单的方式强制它们之间的这种排序(主线程和其他线程的集合)?我必须这样写,我的意思是我不能没有线程。
这就是我想要的输出:
Master Clock : 0
Core 2 started its first task of 7 time units
Core 0 started its first task of 9 time units
Core 1 started its first task of 6 time units
Master Clock : 1
Master Clock : 2
Master Clock : 3
Master Clock : 4
Master Clock : 5
Master Clock : 6
Core 1 started context switch
Master Clock : 7
Core 2 started context switch
Master Clock : 8
Core 1 started a new task of 9 time units
Master Clock : 9
Core 2 started a new task of 10 time units
Core 0 started context switch
Master Clock : 10
Master Clock : 11
Core 0 started a new task of 10 time units
Master Clock : 12
Master Clock : 13
Master Clock : 14
Master Clock : 15
Master Clock : 16
Master Clock : 17
Core 1 started context switch
Master Clock : 18
Master Clock : 19
Core 1 started a new task of 8 time units
Core 2 started context switch
Master Clock : 20
Master Clock : 21
Core 0 completed a total of 2 tasks
Core 2 started a new task of 7 time units
Master Clock : 22
Master Clock : 23
Master Clock : 24
Master Clock : 25
Master Clock : 26
Master Clock : 27
Core 1 completed a total of 3 tasks
Master Clock : 28
Core 2 completed a total of 3 tasks