Getting different thread order even time I run my program

后端 未结 3 1844
醉梦人生
醉梦人生 2021-01-22 09:28

can someone tell me the order in which a thread starts to execute?. I have written the following code

class NewThread implements Runnable {
    Thread t;
    New         


        
3条回答
  •  野的像风
    2021-01-22 09:55

    You have encountered a race condition.

    You have stumbled across one of the complexities of multi-threading. If you have code that MUST be processed in a particular order you must "lock" that code and/or declare variables within it "volatile". Run a google search for "deadlocks" and "race conditions".

提交回复
热议问题