Results 1 to 3 of 3
- 02-14-2011, 01:58 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 61
- Rep Power
- 0
How to release multiple threads from waiting state to runnable state
package Tricky;
class Shared
{
synchronized void test1()
{
System.out.println("test1()->begin");
try
{
System.out.println(Thread.currentThread()+"->is waiting");
wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
synchronized void test2()
{
notify();
}
synchronized void releaseAll()
{
notifyAll();
}
}
class Thread111 extends Thread
{
Shared s1;
Thread111(Shared s1)
{
this.s1 = s1;
}
@Override
public void run()
{
System.out.println("t1->begin");
s1.test1();
System.out.println("t1->end");
}
}
class Thread222 extends Thread
{
Shared s1;
Thread222(Shared s1)
{
this.s1 = s1;
}
@Override
public void run()
{
System.out.println("t2->begin");
try
{
sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
s1.test1();
System.out.println("test2()-> end->execution over");
}
}
class Super
{
public Super()
{
System.out.println("super constructor");
}
void release()
{
Shared s = new Shared();
s.releaseAll();
}
}
public class Mgr2
{
public static void main(String[] args)
{
Shared s1 = new Shared();
Shared s2 = new Shared();
Thread111 t1 = new Thread111(s1);
Thread222 t2 = new Thread222(s2);
t1.start();
t2.start();
try
{
Thread.sleep(6000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
Super sr = new Super();
sr.release();
}
}
Here in the above code when i run the program the below output came
t1->begin
t2->begin
test1()->begin
Thread[Thread-0,5,main]->is waiting
test1()->begin
Thread[Thread-1,5,main]->is waiting
super constructor
Though s.releaseAll(); is called from the Super class the child threads t1 & t2 are still in waiting state(i.e the program never terminates)
can any one please tell me how to make threads t1 & t2 in runnable state
if possible please provide programs(examples) for notify() & notifyAll()
Thanks in advance
Daya
- 02-14-2011, 03:07 PM #2
Hello and welcome!
Please use [code][/code] tags so we can read your code :D
- 02-14-2011, 03:27 PM #3
Also, this seems to be a pretty simple example of what you are talking about, perhaps it will be helpful:
Synchronizing threads in Java, Part 1 - JavaWorld
Similar Threads
-
Fetching tcp socket state
By rohitvarma1 in forum Advanced JavaReplies: 0Last Post: 06-30-2010, 01:14 PM -
JFrame state
By Chasingxsuns in forum New To JavaReplies: 2Last Post: 01-25-2010, 12:03 AM -
Thread RUNNABLE or WAITING
By Pushkar in forum Threads and SynchronizationReplies: 10Last Post: 01-14-2010, 02:36 AM -
Check a key's state
By CodesAway in forum Advanced JavaReplies: 0Last Post: 11-26-2009, 07:09 PM
Bookmarks