Results 1 to 2 of 2

Thread: notifyAll

  1. #1
    simorgh is offline Member
    Join Date
    Jan 2010
    Posts
    25
    Rep Power
    0

    Default notifyAll

    Java Code:
    # public class distanceshow implements Runnable  {  
    #       
    #     /** Creates a new instance of distanceshow */  
    #    int i;  
    #    int d;  
    #       
    #     public static int distance=100;  
    #      
    #        public synchronized void run()  
    #             {  
    #          if(i<2){  
    #           i++;  
    #           System.out.println(" Runner number" +'\t' + i +'\t'+"is ready");  
    #            try{  
    #                 wait ();  
    #               }  
    #          catch (InterruptedException e) { }  
    #          } else if(i<3){  
    #              i++;  
    #              System.out.println(" Runner number" +'\t' + i +'\t'+"is ready"); notifyAll();       
    #              System.out.println(" START");  
    #              notifyAll();  
    #              for ( i=100;i<=400;i+=100){  
    #                 System.out.println(Thread.currentThread().getName()+"distance="+ i );  
    #              }  
    #                    
    #                    
    #             }  
    #       
    #        }  
    # }  
    #   
    #   
    #   
    # public class Main {  
    #       
    #     /** Creates a new instance of Main */  
    #       
    #       
    #         public static void main(String[] args) {  
    #         // TODO code application logic here  
    #         distanceshow d=new distanceshow();  
    #          
    #                    
    #         Thread thread1 = new Thread(d);  
    #         thread1.setName("runner num 1");  
    #         Thread thread2 = new Thread(d);  
    #         thread2.setName("runner num 2");  
    #         Thread thread3 = new Thread(d);  
    #         thread3.setName("runner num 3");  
    #         thread1.start();  
    #         thread2.start();  
    #         thread3.start();   
    #     }  
    #       
    # }


    I want this program print this :


    For example :

    Runner number 1 distance is 100
    Runner number 3distance is 100
    Runner number 2 distance is 100


    and :
    Runner number 3 distance is 200
    Runner number 2 distance is 200
    Runner number 1 distance is 200



    until the 400,The last thread that run this is winner:

    for example :

    Runner number 2 wins!


    May you guide me how can I code for that?Could you write code that can do that?

    Thanks in advance

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Okay, first of all welcome to Java Forums.

    Actually this is forum, not a code bank or a place to get work done for you. You have to put some effort, and then if you stuck on something ask anything. We can help you. Keep in your mind.

    Regards your question, dealing with Threads. Hope you know the basis of Threads. Then think about the logic you are using. The way you print something, and what you print is not clear. Please explain it bit more details.

Similar Threads

  1. about wait() and notifyALL
    By denis in forum Threads and Synchronization
    Replies: 13
    Last Post: 04-22-2009, 08:28 AM

Posting Permissions

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