In the code below, when I execute the producercon
class, sometimes executable stucks, looks like a dead end. But if I do get_flag ()
synchronization then there will be no such problems.
I canβt understand how the problem could be. flag
can be true or false, so only one of producer
or consumer
gets into the if
. After one of them enters if
, it enters the monitor with object r
(both are initialized with the same object reference). The only problem that can happen is that the object r
, which changes when the function increment_decrement ()
called, and get_flag ()
, reads the flag at the same time, but even then it will not enter if
in this iteration, but it will go into the if
block on the next iteration, and even if the first thread does not leave the monitor, it will wait for it there (before the synchronized
block).
How and why does the program stop / hang if get_flag ()
not made synchronized
?
import java.io.*; class resource { private boolean res, flag; resource () { flag=false; } boolean get_flag () { return flag; } void increment_decrement (String s,boolean t) { res=t; flag=t; try { System.out.print("\n"+s+":"+res); Thread.sleep(200); } catch(InterruptedException e) { } } } class producer implements Runnable { resource r1; Thread t1; producer(resource r) { r1 = r; t1 = new Thread(this); t1.start(); } public void run () { while (true) { if(r1.get_flag () == false) { synchronized(r1) { r1.increment_decrement("Producer",true); } } } } public void waitForThread () throws InterruptedException { t1.join (); } } class consumer implements Runnable { resource r2; Thread t2; consumer(resource r) { r2 = r; t2 = new Thread (this); t2.start(); } public void run() { while (true) { if(r2.get_flag () == true) { synchronized(r2) { r2.increment_decrement("Consumer",false); } } } } public void waitForThread () throws InterruptedException { t2.join (); } } public class producercon { public static void main(String args[]) { try { System.out.print("PRESS CTRL+C TO TERMINATE\n"); resource r = new resource(); consumer c = new consumer(r); producer p = new producer(r); c.waitForThread (); p.waitForThread (); } catch(InterruptedException e) { } } }
source share