i wrote code snippet starts 2 threads; 1 thread prints odd numbers while thread prints numbers. used combination of intrinsic lock , thread communication commands achieve proper interleaving of 2 threads. here code,
public class threadevenodd implements runnable { static boolean isodd=true; int count = 10; boolean value; static int c=1; static object lock = new object(); threadevenodd(boolean temp) { value = temp; } public void run() { if(value) { printodd(count); } if(!value) { printeven(count); } } void printodd(int count) { try { for(int i=0;i<count/2;i++) { //system.out.println("odd enters lock"); synchronized(lock) { if(!isodd) { //system.out.println("odd in barrier"); lock.wait(); } system.out.println(c); c++; isodd = false; //system.out.println("odd notifies"); lock.notify(); } } } catch(exception e) { system.out.println(e); } } void printeven(int count) { try { for(int i=0;i<count/2;i++) { //system.out.println("even enters lock"); synchronized(lock) { if(isodd) { //system.out.println("even in barrier"); lock.wait(); } system.out.println(c); c++; isodd = true; //system.out.println("even notifies"); lock.notify(); } } } catch(exception e) { system.out.println(e); } } public static void main (string args[]) { threadevenodd th1 = new threadevenodd(true); threadevenodd th2 = new threadevenodd(false); thread t1 = new thread(th1); t1.setname("odd"); thread t2 = new thread(th2); t2.setname("even"); //system.out.println(t1.getname() + " starts"); t1.start(); //system.out.println(t2.getname() + " starts"); t2.start(); } }
here questions:
the odd thread executes in printodd() function, while thread executes in printeven() function. using 1 intrinsic lock both threads; don't understand how 2 threads can exist in respective synchronized blocks @ same time, because use same lock.
i removed thread communication statements(notify, wait) code , still obtained desired output. wondering if code needs thread communication statements @ all.
i guess still need work on understanding of multithreading concepts, struggling understand own code :p can explain if there better way using multithreading concepts have used?
each thread has own path of execution through code. if 2 threads run exact same code still have 2 distinct execution points through code execution through code. when thread reaches synchronized statement waits lock available - enter synchronized block if no other thread inside synchronized block guarded same lock.
you keep getting same output although removed notify/wait statements can coincidental. did try relatively large value of
count
field?it kind of hard answer question @ moment didn't specify output expect program produce. "1,3,5,7,9,2,4,6,8" valid output? "1,3,2,4,6,5,7,9,8"? or "1,2,3,4,5,6,7,8,9" valid output? said, here few quick points:
use notifyall() instead of notify
minimize state shared between threads. in case, share both
isodd
,c
. note former can computed latter viac % 2 == 1
. can have thread computing oddness instead of maintaining piece of shared data.instead of sharing via static fields create object (with instance fields) , pass object constructor of each thread. can use object lock.
here's how can like:
class shareddata { int c; boolean isodd; } class threadevenodd { shareddata shareddata; pubic threadevenodd(shareddata sd) { this.shareddata = sd } ... void printodd(int count) { try { for(int i=0;i<count/2;i++) { synchronized(shareddata) { if(!shareddata.isodd) { ... } system.out.println(shareddata.c); shareddata.c++; shareddata.isodd = false; lock.notify(); } } } } catch(exception e) { system.out.println(e); } }
the nice thing can start defining real methods on shareddata (such as: method increases c
, set isodd
appropriate value based on value of c
further simplifying code in thread class - , making synchronization/notification less interleaved processing of data, makes code more readable , less prone errors.
Comments
Post a Comment