unwanted output in multithreading

后端 未结 3 1211
旧巷少年郎
旧巷少年郎 2020-12-22 03:10
class mythread implements Runnable {

    Thread t1;
    String name = \"\";

    public mythread(String thname) {
         name = thname;
         t1= new Thread(th         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-22 04:01

    Threads are given processor time or scheduled at the OS level. So your program doesn't have direct control over the times when instructions are executed across its own threads. So there is now guarantee of order. There are a handful of OS specific variables that go into scheduling.

    If you want to guarantee order across threads than your threads need to communicate that. There are a handful of ways to do this. Most commonly programmers use a mutex; which is also called a lock.

提交回复
热议问题