Results 1 to 4 of 4

Thread: wait() and notify()?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Resolved wait() and notify()?

    Below is a simple program which just creates two threads. One creates a file and fills it with the letters of the alphabet. The other opens the same file and reads the contents printing them to the console. Along the way the state of the thread is just printed. The code produces three states NEW, RUNNABLE and TERMINATED but i want to see if i can squeeze in one more state. WAITING or BLOCKED without having to drastically change the structure of the code. Any ideas??? Thanks ahead of time.
    Code:
    import java.io.*; 
     
    public class ThTest2{
     public static void main(String[] args){
      try{
      AlphaBlower ab = new AlphaBlower("AlphaBlower");
      ThreadState(ab);
      ab.start(); 
      ab.join(); 
      ThreadState(ab);
      
      AlphaSucker as = new AlphaSucker("AlphaSucker");
      ThreadState(as); 
      as.start();
      as.join();
      ThreadState(as);
      }catch(InterruptedException ie){
        System.out.println(ie);	   
      }
     }
     public static void ThreadState(Thread t){
       Enum e = t.getState();
       System.out.println(t.getName() + " is " + e.name()); 
     }
    }
    class AlphaBlower extends Thread{
      
     private BufferedWriter br; 
     public AlphaBlower(String tname){
      super(tname);
    
     }
     public void run(){
      ThTest.ThreadState(Thread.currentThread());
      try{
       br = new BufferedWriter(new FileWriter("C:" + File.separator + "AlphaHolder.txt")); 
      }catch(IOException ioe){
        System.err.println(ioe);
      }
      try{
      for(int alpha = 97; alpha <= 122; ++alpha){
       try{
        br.write(alpha);
       }catch(IOException ioe){
          System.err.println(ioe);    
       }
      }
      }finally{
       try{
       br.close();
      }catch(IOException ioe){          
        System.out.println(ioe); 
       }
      }
     }
    }
    
    class AlphaSucker extends Thread{
     private int c; 
     private BufferedReader br;
     public AlphaSucker(String tname){
      super(tname);
     }
     public void run(){
    
      ThTest.ThreadState(Thread.currentThread());
      try{
       br = new BufferedReader(new FileReader("C:" + File.separator + "AlphaHolder.txt")); 
      }catch(IOException ioe){
       System.err.println(ioe);
      }
      try{
      while((c = br.read()) != -1){
      //System.out.print((char)c); 
      }
      }catch(IOException ioe){
         System.err.println(ioe);
      }finally{
       try{
       br.close();
       }catch(IOException ioe){System.err.println(ioe);}
      }
     }
    }

  2. #2

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: How could i change this prog to wait() and notify()?

    I made some changes but i ran into a deadlock.
    Code:
    import java.io.*; 
    
    class AlphaHolder{
     private static boolean available = false;
     public static void setAvailable(boolean a){
      available = a;
     }
     public static boolean getAvailable(){
      return available;
     }
    }
    
    public class ThTest{
     public static void main(String[] args){
      AlphaSucker as = new AlphaSucker("AlphaSucker");
      ThreadState(as); 
      as.start();
      
      AlphaBlower ab = new AlphaBlower("AlphaBlower");
      ThreadState(ab);
      ab.start(); 
     }
     public static void ThreadState(Thread t){
       Enum e = t.getState();
       System.out.println(t.getName() + " is " + e.name()); 
     }
    }
    class AlphaBlower extends Thread{
     private BufferedWriter bw;
     public AlphaBlower(String tname){
      super(tname);
     }
     public synchronized void run(){
      ThTest.ThreadState(Thread.currentThread());
      try{
       bw = new BufferedWriter(new FileWriter("C:" + File.separator + "AlphaHolder.txt")); 
      }catch(IOException ioe){
        System.err.println(ioe);
      }
      try{
      for(int alpha = 97; alpha <= 122; ++alpha){
       try{
    	bw.write(alpha);
       }catch(IOException ioe){
        System.err.println(ioe);    
       }
      }
      }finally{
       try{
        bw.close();
    	AlphaHolder.setAvailable(true);
        notify(); 
       }catch(IOException ioe){          
         System.out.println(ioe); 
       }
      }
     }
    }
    
    class AlphaSucker extends Thread{
     private int c; 
     private BufferedReader br;
     public AlphaSucker(String tname){
      super(tname);
     }
     public synchronized void run(){
      ThTest.ThreadState(Thread.currentThread());
       while(!AlphaHolder.getAvailable()){
        try{
    	 wait();
        }catch(InterruptedException ie){
          System.out.println(ie);	   
        }
       }
      try{
       br = new BufferedReader(new FileReader("C:" + File.separator + "AlphaHolder.txt"));
      }catch(IOException ioe){
       System.err.println(ioe);
      }
      try{
      while((c = br.read()) != -1){
       System.out.print((char)c); 
      }
      }catch(IOException ioe){
        System.err.println(ioe);
      }finally{
       try{
       br.close();
       }catch(IOException ioe){System.err.println(ioe);}
      }
     }
    }

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: How could i change this prog to wait() and notify()?

    No more deadlock since i changed some things. I didn't make sense to have two separate threads each having a synchronized method. Nothing to synchronize on. I added an extra method (which is synchronized) to the AlaphaHolder class. Also an IllegalMonitorStateException is thrown at the notifyAll().
    Code:
    import java.io.*; 
    
    class AlphaHolder{
      private static boolean available = false;
      public static void setAvailable(boolean a){
       available = a;
      }
      public static boolean getAvailable(){
       return available;
      }
      public static synchronized File getFile(){
        return new File("C:" + File.separator + "AlphaHolder.txt");
      }
     }
    
    public class ThTest{
     public static void main(String[] args){
      AlphaSucker as = new AlphaSucker("AlphaSucker");
      ThreadState(as); 
      as.start();
      
      AlphaBlower ab = new AlphaBlower("AlphaBlower");
      ThreadState(ab);
      ab.start(); 
     }
     public static void ThreadState(Thread t){
       Enum e = t.getState();
       System.out.println(t.getName() + " is " + e.name()); 
        
     }
    }
    class AlphaBlower extends Thread{
     private BufferedWriter bw;
     public AlphaBlower(String tname){
      super(tname);
     }
     public void run(){
      ThTest.ThreadState(Thread.currentThread());
      try{
       bw = new BufferedWriter(new FileWriter(AlphaHolder.getFile())); 
      }catch(IOException ioe){
        System.err.println(ioe);
      }
      try{
      for(int alpha = 97; alpha <= 122; ++alpha){
       try{
    	bw.write(alpha);
       }catch(IOException ioe){
    	System.err.println(ioe);    
       }
      }
      }finally{
       try{
        bw.close();
    	AlphaHolder.setAvailable(true);
        notifyAll(); 
       }catch(IOException ioe){          
        System.out.println(ioe); 
       }
      }
     }
    }
    
    class AlphaSucker extends Thread{
     private int c; 
     private BufferedReader br;
     public AlphaSucker(String tname){
      super(tname);
     }
     public void run(){
      ThTest.ThreadState(Thread.currentThread());
      while(!AlphaHolder.getAvailable()){
      try{
       wait();
       }catch(InterruptedException ie){;}
      }
      try{
       br = new BufferedReader(new FileReader(AlphaHolder.getFile()));
      }catch(IOException ioe){
       System.err.println(ioe);
      }
      try{
      while((c = br.read()) != -1){
       System.out.print((char)c); 
      }
      }catch(IOException ioe){
       System.err.println(ioe);
      }finally{
       try{
       br.close();
       }catch(IOException ioe){System.err.println(ioe);}
      }
     }
    }

  4. #4

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: How could i change this prog to wait() and notify()?

    Got the wait() and notify() to work but there seems no way to get one more thread state printed, namley WAITING.
    Code:
    import java.io.*; 
    
    class AlphaHolder{
     private boolean available;
     public synchronized void write(){
       BufferedWriter bw = null;
       try{
       bw = new BufferedWriter(new FileWriter("C:" + File.separator + "AlphaHolder.txt")); 
      }catch(IOException ioe){
        System.err.println(ioe);
      }
      try{
      for(int alpha = 97; alpha <= 122; ++alpha){
       try{
    	bw.write(alpha);
       }catch(IOException ioe){
    	System.err.println(ioe);    
       }
      }
      }finally{
       try{
        bw.close();
    	available = true;
    	notifyAll(); 
       }catch(IOException ioe){          
        System.out.println(ioe); 
       }
      }
     }
    
     public synchronized void read(){
      while(!available){
       try{
       wait();
       }catch(InterruptedException ie){;}
      }
       int c; 
       BufferedReader br = null;
       try{
       br = new BufferedReader(new FileReader("C:" + File.separator + "AlphaHolder.txt"));
      }catch(IOException ioe){
       System.err.println(ioe);
      }
      try{
      while((c = br.read()) != -1){
       System.out.print((char)c); 
      }
      }catch(IOException ioe){
       System.err.println(ioe);
      }finally{
       try{
       br.close();
       }catch(IOException ioe){System.err.println(ioe);}
      }
     }
    }
    
    public class ThTest{
     public static void main(String[] args){
      AlphaHolder ah = new AlphaHolder(); 
     
      AlphaSucker as = new AlphaSucker("AlphaSucker",ah);
      ThreadState(as); 
      as.start();
    
      AlphaBlower ab = new AlphaBlower("AlphaBlower",ah);
      ThreadState(ab);
      ab.start();
     }
     public static void ThreadState(Thread t){
       Enum e = t.getState();
       System.out.println(t.getName() + " is " + e.name()); 
        
     }
    }
    class AlphaBlower extends Thread{
     
     private AlphaHolder ah; 
     public AlphaBlower(String tname, AlphaHolder ah){
      super(tname);
      this.ah = ah;
     }
     public void run(){
      ThTest.ThreadState(Thread.currentThread());
      ah.write();
     }
    }
    
    class AlphaSucker extends Thread{
     private AlphaHolder ah; 
     public AlphaSucker(String tname, AlphaHolder ah){
      super(tname);
      this.ah = ah; 
     }
     public void run(){
      ThTest.ThreadState(Thread.currentThread());
      ah.read(); 
     }
    }

Posting Permissions

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



Click Here to Expand Forum to Full Width