Results 1 to 1 of 1
Thread: thread synchronisation
- 04-01-2009, 05:11 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 5
- Rep Power
- 0
thread synchronisation
hi,
i wanna to implement this
a class that implement this :waiting() and wakeup() ,we call it event
we have only one event shared between so many threads
dynamic number of thread r executing waiting
and one other notify them by executing wakeup to notify all the thread which r waiting for this notification
for exemple look at this
class a {
public int a;=0;
event e=new event();
public a(){
callThread(); //creating thread 1
callThread(); //creating thread 2
callThread(); //creating thread 3
callThread(); //creating thread 4
callThread(); //creating thread 5
//this thread will notify them
new Thread (){
run(){
a=5;
e.wakeup()
}}.start();
}
//this methode will create and start a thread that will wait for a notification
// and after that it will print the value of the variable a
private void callThread(){
new Thread (){
run(){
e.waitng();
System.out.println(a);
}}.start();
}
normally all the threads will not print a only after the thread will notify all of them
the problem that i have
only one could see the notification
because as u see this is the class event
public final class Event {
/**
* variable used to store the notification if it takes place
* before being expected
*/
private boolean status = false;
public final synchronized void waiting() {
while (status == false ) {
try {
wait();
}
catch (InterruptedException e) {
}
}
status = false ;
}
// notify all waits on this event
public final synchronized void wakeUp() {
status = true;
notifyAll();
}
}
when the thread send notification the flag is set to true and only one could see that modification and go out from the wait status and modify the flag to false so the others always see the flag false so they could go out from waiting
Similar Threads
-
Difference between Thread.yield() and Thread.sleep() methods
By Nageswara Rao Mothukuri in forum New To JavaReplies: 12Last Post: 07-30-2010, 05:37 PM -
data from the main/GUI thread to another runnin thread...
By cornercuttin in forum Threads and SynchronizationReplies: 2Last Post: 04-23-2008, 10:30 PM -
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks