Results 1 to 3 of 3
  1. #1
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default 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

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    Hello and welcome!
    Please use [code][/code] tags so we can read your code :D

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    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

  1. Fetching tcp socket state
    By rohitvarma1 in forum Advanced Java
    Replies: 0
    Last Post: 06-30-2010, 12:14 PM
  2. JFrame state
    By Chasingxsuns in forum New To Java
    Replies: 2
    Last Post: 01-24-2010, 11:03 PM
  3. Thread RUNNABLE or WAITING
    By Pushkar in forum Threads and Synchronization
    Replies: 10
    Last Post: 01-14-2010, 01:36 AM
  4. Check a key's state
    By CodesAway in forum Advanced Java
    Replies: 0
    Last Post: 11-26-2009, 06:09 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •