Execution order of multiple threads

前端 未结 4 1774
失恋的感觉
失恋的感觉 2020-12-03 16:43

Let\'s say we have this scenario:

class Stack{

public void main{

ChildThread1 t1 = new ChildThread1;
ChildThread1 t2 = new ChildThread1;
ChildThread1 t3 =          


        
相关标签:
4条回答
  • 2020-12-03 16:51

    When you start a thread, the started thread runs in parallel of all the already running threads. The thread scheduler dispatches the various threads on the available processors, and each thread gets some processor time, each in his turn. But the processor, the order and the time assigned to each thread is up to the OS thread scheduler, and you have absolutely no guarantee.

    0 讨论(0)
  • 2020-12-03 16:53

    So am I right to assume that this weird order of prints is because start() does not guarantee order of execution?

    Yes. You are right.

    Will this reason also apply to 'all threads are ready' problem?

    Yes. Right again. Your SOP is run by main thread. So, it's possible that t1 can print something before main gets chance to execute it's SOP.

    0 讨论(0)
  • 2020-12-03 16:55

    The whole point of threads is that they can be executed concurrently. If you want to ensure specific order in which things are done, you either have to move away from using threads, or use explicit synchronization.

    So am I right to assume that this weird order of prints is because start() does not guarantee order of execution?

    That's right. When you start a thread, there's basically a race condition between the main thread and the newly created thread. This means that nothing can be said about the relative order in which things happen between the two threads. If you want to ensure specific ordering, use synchronization.

    0 讨论(0)
  • 2020-12-03 17:00

    Easy way to maintain ordering in Thread Execution is to use Semaphore

    public class Semaphore {
    
    int value;
    
    public Semaphore(int intialValue) {
        this.value = intialValue;
    }
    
    public synchronized void p() {
        while (value <= 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
            }
    
        }
        value = value - 1;
    }
    
    public synchronized void v() {
        value = value + 1;
        this.notify();
    }
    

    }

    public class ThreadSync {
    
    static Semaphore semaphore = new Semaphore(0);
    
    public static void main(String[] args) {
        // t1 should be executed before t2
        Thread t1 = new Thread(new Runnable() {
    
            @Override
            public void run() {
                semaphore.p();
                System.out.println("executing " + Thread.currentThread().getName());
            }
        });
        Thread t2 = new Thread(new Runnable() {
    
            @Override
            public void run() {
                System.out.println("executing " + Thread.currentThread().getName());
                semaphore.v();
            }
        });
    
        t1.setName("Thread 1");
        t2.setName("Thread 2");
        t2.start();
        t1.start();
    
    }
    

    }

    0 讨论(0)
提交回复
热议问题