Results 1 to 4 of 4

Thread: wait() and notify()?

Threaded View

  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);}
      }
     }
    }

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