I want to implement following in java code :
Main thread
wait for completion of stage 2 of all three threads
Access all three local variable (LC0, LC1, LC2) of threads
bulid a new main data (MD)
Resume all three threads execution
Wait for end of each thread
Acess all three local variable (LC'0, LC'1, LC'2)
Each threads executes following1. Build a local variable (LC) 2. wait till main thread signals in stage 5 (of main) 3. access a main data (MD) 4. update local data (LC'= updated LC) 5. do something with local data (LC') 6. end
My problems:
There is no need of synchronization. hence i can't use wait and notify.
How to access local data of threads in the when threads is paused ( is waiting).
How to resume execution of all three threads.
I will expain the algorithm if required.
Please help me.
so my whole day has to be wasted for answer by ME ONLY.
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package thread.work; import com.sun.corba.se.spi.monitoring.MonitoredAttribute; import com.sun.corba.se.spi.monitoring.MonitoredObject; import java.util.Collection; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author MKJ */ class mods{ static Object mo1=new Object(); static Object mo2=new Object(); boolean wasSignalled = false; public void doWait(Object mo){ synchronized(mo){ if(!wasSignalled){ try { mo.wait(); } catch (InterruptedException ex) { Logger.getLogger(mods.class.getName()).log(Level.SEVERE, null, ex); } //clear signal and continue running. wasSignalled = false; } } } public void doNotify(Object mo){ synchronized(mo){ wasSignalled = true; mo.notify(); } } void foo1(Integer x) throws InterruptedException{ System.out.println("before notify foo1["+x+"]"); doNotify(mo1); System.out.println("before wait foo1["+x+"]"); doWait(mo2); System.out.println("after wait foo1["+x+"]"); } void foo2(Integer arr[]) throws InterruptedException{ for (int i = 0; i < arr.length; i++) { System.out.println("before wait foo2"); doWait(mo1); System.out.println("after wait foo2"); } for (Integer integer : arr) { System.out.print(" ["+integer+" ]"); } for (int i = 0; i < arr.length; i++) { doNotify(mo2); } } } class ChildThread implements Runnable { Thread t; Integer ld; mods theO; public ChildThread(Integer var,mods theO) { this.theO=theO; ld=var; t=new Thread(this, "child"+ld); t.start(); } @Override public void run() { try { theO.foo1(ld); } catch (InterruptedException ex) { Logger.getLogger(ChildThread.class.getName()).log(Level.SEVERE, null, ex); } } } class MasterThread implements Runnable{ Thread t; Integer arr[]; mods theO; public MasterThread(Integer var[],mods theO) { this.theO=theO; arr=var; for (Integer integer : arr) { System.out.println(" ["+integer+" ]"); } t=new Thread(this,"master"); t.start(); } @Override public void run() { try { theO.foo2(arr); } catch (InterruptedException ex) { Logger.getLogger(ChildThread.class.getName()).log(Level.SEVERE, null, ex); } } } public class ThreadWork { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here ChildThread ft[]; MasterThread mt; mods theMod=new mods(); Integer ints[]=new Integer[3]; ints[0]=0; ints[1]=1; ints[2]=2; ft = new ChildThread[3]; mt=new MasterThread(ints,theMod); for (int i = 0; i < 3; i++) { ft[i]=new ChildThread(ints[i],theMod); } } }
so my whole day has to be wasted for answer by ME ONLY.
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package thread.work; import com.sun.corba.se.spi.monitoring.MonitoredAttribute; import com.sun.corba.se.spi.monitoring.MonitoredObject; import java.util.Collection; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author MKJ */ class mods{ static Object mo1=new Object(); static Object mo2=new Object(); boolean wasSignalled = false; public void doWait(Object mo){ synchronized(mo){ if(!wasSignalled){ try { mo.wait(); } catch (InterruptedException ex) { Logger.getLogger(mods.class.getName()).log(Level.SEVERE, null, ex); } //clear signal and continue running. wasSignalled = false; } } } public void doNotify(Object mo){ synchronized(mo){ wasSignalled = true; mo.notify(); } } void foo1(Integer x) throws InterruptedException{ System.out.println("before notify foo1["+x+"]"); doNotify(mo1); System.out.println("before wait foo1["+x+"]"); doWait(mo2); System.out.println("after wait foo1["+x+"]"); } void foo2(Integer arr[]) throws InterruptedException{ for (int i = 0; i < arr.length; i++) { System.out.println("before wait foo2"); doWait(mo1); System.out.println("after wait foo2"); } for (Integer integer : arr) { System.out.print(" ["+integer+" ]"); } for (int i = 0; i < arr.length; i++) { doNotify(mo2); } } } class ChildThread implements Runnable { Thread t; Integer ld; mods theO; public ChildThread(Integer var,mods theO) { this.theO=theO; ld=var; t=new Thread(this, "child"+ld); t.start(); } @Override public void run() { try { theO.foo1(ld); } catch (InterruptedException ex) { Logger.getLogger(ChildThread.class.getName()).log(Level.SEVERE, null, ex); } } } class MasterThread implements Runnable{ Thread t; Integer arr[]; mods theO; public MasterThread(Integer var[],mods theO) { this.theO=theO; arr=var; for (Integer integer : arr) { System.out.println(" ["+integer+" ]"); } t=new Thread(this,"master"); t.start(); } @Override public void run() { try { theO.foo2(arr); } catch (InterruptedException ex) { Logger.getLogger(ChildThread.class.getName()).log(Level.SEVERE, null, ex); } } } public class ThreadWork { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here ChildThread ft[]; MasterThread mt; mods theMod=new mods(); Integer ints[]=new Integer[3]; ints[0]=0; ints[1]=1; ints[2]=2; ft = new ChildThread[3]; mt=new MasterThread(ints,theMod); for (int i = 0; i < 3; i++) { ft[i]=new ChildThread(ints[i],theMod); } } }
Ads