Wednesday, 15 July 2015

java - Synchronization using wait and signal methods -



java - Synchronization using wait and signal methods -

the java implementation of semaphore using manual wait , signal method not seem working. can wrong?

class runner extends thread implements runnable{ public static int s=1; private static int c; private string tname; runner(){ tname=this.getname(); } public void wait(int s){ while(s==0) system.out.println(tname+" waiting; s = "+s); s--; system.out.println(tname+" wait over; s = "+s); } public void signal(int s){ s++; system.out.println(tname+" signalled; s ="+s); } public void run(){ wait(s); //critical section begin go(); //critical section end signal(s); } public void go(){ int f=10; while(f-->0){ c++; system.out.println(tname+" : counter = "+c); } } } public class wns{ public static void main(string[] args){ runner t1=new runner(); runner t2=new runner(); t1.start(); t2.start(); } }

i ran on ubuntu 14.04 lts , got unexpected output. output

thread-1 wait over; s = 0 thread-0 wait over; s = 0 thread-0 : counter = 2 thread-0 : counter = 3 thread-0 : counter = 4 thread-0 : counter = 5 thread-0 : counter = 6 thread-0 : counter = 7 thread-0 : counter = 8 thread-0 : counter = 9 thread-0 : counter = 10 thread-0 : counter = 11 thread-0 signalled; s =2 thread-1 : counter = 1 thread-1 : counter = 12 thread-1 : counter = 13 thread-1 : counter = 14 thread-1 : counter = 15 thread-1 : counter = 16 thread-1 : counter = 17 thread-1 : counter = 18 thread-1 : counter = 19 thread-1 : counter = 20 thread-1 signalled; s =2

the value of s comes 2 when it's beingness incremented once. likewise when decremented becomes 0 1 time again no effect on synchronization @ all. can explain me what's happening here?

neither thread waiting.

both threads come in wait() method @ same time. both threads check see if s == 0 @ same time. both threads decide that, yes, s equal 0, @ same time. both threads decrement s @ same time. decrement not atomic operation , both threads trying @ exact same time possible 1 of decrements takes hold. (thus s 0 after both have called decrement). both threads phone call signal @ same time. both threads phone call increment operator @ same time. time both increments happen take hold , s 2.

true semaphores , mutexes typically require special processor calls atomic test-and-set operation. java not give access operation (although synchronized mechanism certainly uses under hood) , thus, cannot write own semaphore class without using kind of java locking mechanism (e.g. synchronized).

edit: wrong 1 thing. java allow access compareandset method atomic primitive wrappers (e.g. atomicinteger) located in java.util.concurrent.atomic package. can utilize these create own semaphore without using synchronized keyword.

example: drive point home, if s atomicinteger replace while loop with:

boolean waiting = true; while(waiting) { int stablesvalue = s.get(); if(stablesvalue == 0) { system.out.println("waiting. s 0"); } else { if(s.compareandset(stablesvalue, stablesvalue-1)) { system.out.println("wait done."); waiting = false; } else { system.out.println("optimistic locking failure. trying again."); } } }

java multithreading semaphore

No comments:

Post a Comment