Java Thread yield() vs join() vs sleep()

https://stackoverflow.com/questions/9700871/what-is-difference-between-sleep-method-and-yield-method-of-multi-threading

We can prevent a thread from execution by using any of the 3 methods of Thread class:

  • yield()
  • join()
  • sleep()

yield() method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute. If there is no waiting thread or all the waiting threads have a lower priority then the same thread will continue its execution. The yielded thread when it will get the chance for execution is decided by the thread scheduler whose behavior is vendor dependent (i.e., yield() can only make a heuristic attempt to suspend the execution of the current thread with no guarantee of when will it be scheduled back).

join() The current thread can invoke join() on any other thread which makes the current thread wait for the other thread to die before proceeding. If any executing thread t1 calls join() on t2 (i.e, t2.join()) immediately t1 will enter into waiting state until t2 completes its execution.

sleep() method can force the scheduler to suspend the execution of the current thread for a specified period of time as its parameter.

Java Thread Synchronization

Thread Synchronization

https://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html

    • Mutex Semaphore (aka Mutex) controls only one thread at a time executing on the shared resource by lock & unlock.
    • Counting Semaphore (aka Semaphore – Java7) controls the number of threads executing on the shared resource by acquire & release.
    • Monitor controls only one thread at a time, and can execute in the monitor (shared object) by wait & notify/notifyAll.

Thread Mutex vs NoMutex

Thread with Mutex (cpp11)
thread-cpp11-mutex
– threadFunc에서 mutex를 사용하여 global variable 인 count 가 정상적으로 증가하면서 출력함

Thread without Mutex (cpp11)
thread-cpp11-nomutex
– threadFunc에서 mutex를 사용하지 않고 global variable인 count 를 증가하면서 출력을 했으므로, 정상적인 순서대로 동작하지 않음

Thread with Mutex (cpp11)
thread-cpp11-sharedcounter
–threadFunc에서 mutex를 사용하여 SharedCounter 가 정상적으로 증가하면서 출력함

Java Thread Synchronization
multithread-java-synchronized
Monitor를 사용하여 SharedCounter 가 정상적으로 증가하면서 출력함

Java Thread No Synchronization
multithread-java-notsynchronized
– Monitor를 사용하지 않고 SharedCounter 를 증가하면서 출력을 했으므로, 정상적인 순서대로 동작하지 않음

Lamport vs Vector Clock

4 processes (P1, P2, P3, P4) with events a,b,c,d,e,f,g,…

Multiprocess-Events

Lamport Clock Timestamps
https://en.wikipedia.org/wiki/Lamport_timestamps

For each process, p:
    initialize the timestamp, TS = 0;
    on each event, e:
        if e is receiving message m, 
            p.TS = max(m.TS, p.TS);
        p.TS++; 
        e.TS = p.TS;
        if e is sending message m, 
            m.TS = p.TS;

Lamport-Clock-Timestamp

Vector Clock Timestamps
https://en.wikipedia.org/wiki/Vector_clock

For M processes:
    initialize the timestamp, p.VT = (0,0,..,0);
    on each event, e:
        if e is receiving message m, 
            for i=1 to M, 
                p.VT[i] = max(m.VT[I], p.VT[I]);
        p.VT[self]++; 
        e.VT = p.VT;
        if e is sending message m, 
            m.VT = p.VT;

Vector-Clock-Timestamps