wait() and notify() throw exception even if i have the lock

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cozsmin
    New Member
    • Mar 2007
    • 14

    wait() and notify() throw exception even if i have the lock

    hello , as u know wait() and notify() will not thow an exception if the method that calls them has the lock , or esle i misundrestood java :P





    this is the code that throws (unwanted) exceptions :
    (or u can download it from :
    Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!

    at this link it has the spaces )

    Code:
    public class WaitingForThread {
    
    	public static void main (String []args) {
    
    		String []message = {"msg 1","msg 2","msg 3"
    			,"msg 4","msg 5","msg 6","msg 7","msg 8","msg 9","msg 10"};
    
    		boolean begin[] = new boolean [10];
    
    		Client client = new Client(begin);
    
    		Producer producer = new Producer(message,client,begin);
    
    		new Thread(client).start();
    
    		new Thread(producer).start();
    	}
    }
    
    class Producer implements Runnable {
    
    	String message[] = new String [10];
    
    	int msgNumber;
    
    	Client client;
    
    	boolean begin[];
    
    	Producer (String [] message, Client client,boolean [] begin) {
    
    		this.message = message;
    
    		this.client = client;
    
    		this.begin = begin;
    	}
    
    	public void run() {
    
    		synchronized (this.client) {
    
    			while (msgNumber <message.length) {
    
    				try {
    
    					Thread.sleep(200);
    				}
    					catch(Exception e) {e.printStackTrace();}
    
    				client.message[msgNumber] = message[msgNumber];
    
    				System.out.print("\n\n Producer passed message nr "
    
    				+msgNumber);
    
    				begin[msgNumber] = true;
    
    			
    				try {
    					
    					notify();
    					System.out.print("\n\n the producer waits now..");
    					wait();
    				}
    				
    				catch (Exception e) {e.printStackTrace();}
    
    				msgNumber++;
    			}
    	
    		
    	/*		while(true) {
    				
    				try {
    					
    					Thread.sleep(200);
    					System.out.print("\n\n producer `s run()");
    				}			
    					catch (Exception e) {};
    			}
    			
    	*/
    			
    		}
    	}
    } 
    
    class Client implements Runnable{
    
    	int msgNumber;
    
    	boolean begin[];
    
    	String [] message = new String[10];
    
    	Client(boolean []begin) {
    
    		this.begin = begin;
    	}
    
    	synchronized public void run () {
    
    		while (true) {
     
    			while (!begin[msgNumber]) {
    							
    				try {
    						System.out.print("\n\n the client is waiting for the Producer\n\n");
    		//				System.out.print("\n\n begin["+msgNumber+"] = "+begin[msgNumber]);
    						Thread.sleep(3000);
    						notify();
    						wait();
    					}
    				
    					catch (Exception e) {e.printStackTrace();}
    			}
    
    			if (message[msgNumber]!=null) {
    
    				System.out.print("\n\n client is thinking..");
    
    				try {
    
    					Thread.sleep(2000);
    				}
    
    				catch(Exception e) {e.printStackTrace() ;}
    			
    				System.out.print("\n\n client says : \""+message[msgNumber++]
    				+"\"\n\n");
    
    				try {
    
    					notify();					
    					System.out.print("\n\n client is waiting..");
    					wait();
    				}
    				
    				catch (Exception e) {e.printStackTrace();}
    			}
     
    	
    	
    	/*		while(true) {
    				
    				try {
    					
    					Thread.sleep(200);
    					System.out.print("\n\n clinet `s run ()");
    				}			
    					catch (Exception e) {};
    			}
    	
    	*/
    	
    		}
    	}
    }
    i put the /* */ to see if the 2 run() were synchronidez and they were, too se , just put /* */ the rest of the run()1s body for each class , and u will see that only one run() will print.
    Last edited by JosAH; Apr 3 '07, 03:39 PM. Reason: added code tags for readability
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Which Exception has been thrown? Can you produce a stack trace?

    kind regards,

    Jos

    Comment

    • cozsmin
      New Member
      • Mar 2007
      • 14

      #3
      java.lang.Illeg alMonitorStateE xception

      at java.lang.Objec t.notify(Native Method)
      at Producer.run(Wa itingForThread. java 63) // it wa 64 but i took out the package
      at java.lang.Threa d.run(Unkown Source)


      it has the stackTrace built in th script ;
      just run the code and see

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        I found it: in Java you always synchronize, wait and notify on a certain object.
        Your code waits on the client object (that's why you pass the client to the
        Producer's constructor).

        In your Producer you don't wait/notify on the client, i.e. you simply do this:

        Code:
        wait();
        notify();
        instead of:
        Code:
        client.wait();
        client.notify();
        kind regards,

        Jos

        Comment

        • cozsmin
          New Member
          • Mar 2007
          • 14

          #5
          that is it, instead of .wait() or .notify() on the object i was locked on ( producer is syncronized on client) , i did this.notify() and this.wait()

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by cozsmin
            that is it, instead of .wait() or .notify() on the object i was locked on ( producer is syncronized on client) , i did this.notify() and this.wait()
            Yep, that's it; problem solved ;-)

            kind regards,

            Jos

            Comment

            Working...