Home arrow static arrow Java Programming [Archive] - Shard variable between threads
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Shard variable between threads
This topic has 6 replies on 1 page.

Posts:37
Registered: 7/31/04
Shard variable between threads  
Aug 6, 2004 3:40 AM



 
if i have three process how i can implemnt these threds to share the same variable like this:

Process1{
read (X)
X=x+x
// generate Signal to process 2 and three tell them theat x is ready
Writ (x)
}

Process2{
read (Y)
//Wait until the first process generate signal the is x redy
Y=Y+X /// X value after apply the computation in the first process
// generate Signal to process 3 tell them theat y is ready
Writ (Y)
}

Process3{
read (Z)
//Wait until the X and Y is ready
Z=X+Y+z //// Z and Y and X value after apply the computation in the first and second process
Writ (Z)
}
as you see the second process depend on the value of X in the first process
and the third process depend on the x and Y and the previous process how i can implemnt these process work in paralle ( the previous code is Pescode "not refer to any language")
 

Posts:18,384
Registered: 21.03.00
Re: Shard variable between threads  
Aug 6, 2004 3:49 AM (reply 1 of 6)



 
Hi,

Do you mean threads or processes? If thet are processes you need to use some kind of communication buss (could be sockets), if it is threads, you could solve it with listeners, and synchronization with wait and notify.

/Kaj
 

Posts:37
Registered: 7/31/04
Re: Shard variable between threads  
Aug 6, 2004 3:53 AM (reply 2 of 6)



 
yes i mean thread but how i can let the variable X,Y,Z shared and notify just one process not notifyall and not let another thread to use them until they be redy
 

Posts:6,487
Registered: 5/5/04
Re: Shard variable between threads  
Aug 6, 2004 4:08 AM (reply 3 of 6)



 
yes i mean thread but how i can let the variable X,Y,Z shared
The avriables are shared by default.

and notify just one process not notifyall
Use notify() instead of notifyAll(), note there is no guarentee than another thread will be notified.

and not let another thread to use them until they be ready
Make the methods 'synchronized'
 

Posts:37
Registered: 7/31/04
Re: Shard variable between threads  
Aug 6, 2004 4:24 AM (reply 4 of 6)



 
how process three can acces the X and Y in the process one and two after modefied
and wat y mean it not gerantee to notify thread .....
are y mean i cant notfy one specifc thread;
and which method must be Make it 'synchronized'
 

Posts:37
Registered: 7/31/04
Re: Shard variable between threads  
Aug 6, 2004 4:42 AM (reply 5 of 6)



 
how i can let the thread to determin which thread it mst notify after i wait it ..

procces1 just notify process2 and process3
process2 just notif process3
 

Posts:6,487
Registered: 5/5/04
Re: Shard variable between threads  
Aug 6, 2004 5:01 AM (reply 6 of 6)



 
how process three can acces the X and Y in the process one and two after modefied
Moth threads need a refrence to the same object. Nothing special need sto be done to share objects. If you want ThreadLocal objects, this is more complicated....

and wat y mean it not gerantee to notify thread .....
If you call notify() there is no guarentee that a thread will hear it. If a thread is wait()*** one thread should wake up. However you cannot determine which thread will hear it nor can you ensure that another is waiting before sending a notify. i.e. if other threads happen to not be wait()*** even momentarily, the notify is lost.

are y mean i cant notfy one specifc thread;
yes.

and which method must be Make it 'synchronized'
e.g.
public synchronized void setX(Object X) {    if (this.X  != null)        throw new IllegalStateException("Cannot set X which X is set."):     this.X = X;    notifyAll();} public synchronized Object waitUntilXSet() {     while(true) {         if (X != null) {               Object X2 = X;                X = null;                return X2;          }           try {                wait(TIME_OUT);           } catch(InterruptedException ie) {                 ie.printStackTrace();           }        }}
 
This topic has 6 replies on 1 page.