Ordering threads to run in the order they were created/started

前端 未结 10 1226
礼貌的吻别
礼貌的吻别 2020-11-30 06:15

How can i order threads in the order they were instantiated.e.g. how can i make the below program print the numbers 1...10 in order.

public class ThreadOrder         


        
10条回答
  •  悲哀的现实
    2020-11-30 06:56

    Simply put, the scheduling of threads is indeterminate.

    http://www.janeg.ca/scjp/threads/scheduling.html Dead domain - do not click

    WaybackMachine version of the above page

    The longer answer is that if you want to do this, you'll need to manually wait for each thread to complete its work before you allow another to run. Note that in this fashion, all the threads will still interleave but they won't accomplish any work until you give the go-ahead. Have a look at the synchronize reserved word.

提交回复
热议问题