473,326 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

14
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 :
http://www.geocities.com/mndt_0/file...ForThread.java
at this link it has the spaces )

Expand|Select|Wrap|Line Numbers
  1. public class WaitingForThread {
  2.  
  3.     public static void main (String []args) {
  4.  
  5.         String []message = {"msg 1","msg 2","msg 3"
  6.             ,"msg 4","msg 5","msg 6","msg 7","msg 8","msg 9","msg 10"};
  7.  
  8.         boolean begin[] = new boolean [10];
  9.  
  10.         Client client = new Client(begin);
  11.  
  12.         Producer producer = new Producer(message,client,begin);
  13.  
  14.         new Thread(client).start();
  15.  
  16.         new Thread(producer).start();
  17.     }
  18. }
  19.  
  20. class Producer implements Runnable {
  21.  
  22.     String message[] = new String [10];
  23.  
  24.     int msgNumber;
  25.  
  26.     Client client;
  27.  
  28.     boolean begin[];
  29.  
  30.     Producer (String [] message, Client client,boolean [] begin) {
  31.  
  32.         this.message = message;
  33.  
  34.         this.client = client;
  35.  
  36.         this.begin = begin;
  37.     }
  38.  
  39.     public void run() {
  40.  
  41.         synchronized (this.client) {
  42.  
  43.             while (msgNumber <message.length) {
  44.  
  45.                 try {
  46.  
  47.                     Thread.sleep(200);
  48.                 }
  49.                     catch(Exception e) {e.printStackTrace();}
  50.  
  51.                 client.message[msgNumber] = message[msgNumber];
  52.  
  53.                 System.out.print("\n\n Producer passed message nr "
  54.  
  55.                 +msgNumber);
  56.  
  57.                 begin[msgNumber] = true;
  58.  
  59.  
  60.                 try {
  61.  
  62.                     notify();
  63.                     System.out.print("\n\n the producer waits now..");
  64.                     wait();
  65.                 }
  66.  
  67.                 catch (Exception e) {e.printStackTrace();}
  68.  
  69.                 msgNumber++;
  70.             }
  71.  
  72.  
  73.     /*        while(true) {
  74.  
  75.                 try {
  76.  
  77.                     Thread.sleep(200);
  78.                     System.out.print("\n\n producer `s run()");
  79.                 }            
  80.                     catch (Exception e) {};
  81.             }
  82.  
  83.     */
  84.  
  85.         }
  86.     }
  87.  
  88. class Client implements Runnable{
  89.  
  90.     int msgNumber;
  91.  
  92.     boolean begin[];
  93.  
  94.     String [] message = new String[10];
  95.  
  96.     Client(boolean []begin) {
  97.  
  98.         this.begin = begin;
  99.     }
  100.  
  101.     synchronized public void run () {
  102.  
  103.         while (true) {
  104.  
  105.             while (!begin[msgNumber]) {
  106.  
  107.                 try {
  108.                         System.out.print("\n\n the client is waiting for the Producer\n\n");
  109.         //                System.out.print("\n\n begin["+msgNumber+"] = "+begin[msgNumber]);
  110.                         Thread.sleep(3000);
  111.                         notify();
  112.                         wait();
  113.                     }
  114.  
  115.                     catch (Exception e) {e.printStackTrace();}
  116.             }
  117.  
  118.             if (message[msgNumber]!=null) {
  119.  
  120.                 System.out.print("\n\n client is thinking..");
  121.  
  122.                 try {
  123.  
  124.                     Thread.sleep(2000);
  125.                 }
  126.  
  127.                 catch(Exception e) {e.printStackTrace() ;}
  128.  
  129.                 System.out.print("\n\n client says : \""+message[msgNumber++]
  130.                 +"\"\n\n");
  131.  
  132.                 try {
  133.  
  134.                     notify();                    
  135.                     System.out.print("\n\n client is waiting..");
  136.                     wait();
  137.                 }
  138.  
  139.                 catch (Exception e) {e.printStackTrace();}
  140.             }
  141.  
  142.  
  143.  
  144.     /*        while(true) {
  145.  
  146.                 try {
  147.  
  148.                     Thread.sleep(200);
  149.                     System.out.print("\n\n clinet `s run ()");
  150.                 }            
  151.                     catch (Exception e) {};
  152.             }
  153.  
  154.     */
  155.  
  156.         }
  157.     }
  158. }
  159.  
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.
Apr 3 '07 #1
5 7526
JosAH
11,448 Expert 8TB
Which Exception has been thrown? Can you produce a stack trace?

kind regards,

Jos
Apr 3 '07 #2
cozsmin
14
java.lang.IllegalMonitorStateException

at java.lang.Object.notify(Native Method)
at Producer.run(WaitingForThread.java 63) // it wa 64 but i took out the package
at java.lang.Thread.run(Unkown Source)


it has the stackTrace built in th script ;
just run the code and see
Apr 3 '07 #3
JosAH
11,448 Expert 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. wait();
  2. notify();
instead of:
Expand|Select|Wrap|Line Numbers
  1. client.wait();
  2. client.notify();
kind regards,

Jos
Apr 3 '07 #4
cozsmin
14
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()
Apr 3 '07 #5
JosAH
11,448 Expert 8TB
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
Apr 3 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: arun gunda | last post by:
I want to throw exception dynamically. This what I want to do For example I want to throw System.Net.WebException exception. I will know the full exception name at run time, can I create a...
3
by: Kerri | last post by:
Hi, I am new to .NET In my Error Logic on my Aspx pages when an error happens it hits my catch statement where I throw an Exception. My question is : what is the difference between Thwo...
2
by: Dave | last post by:
Josuttis states that I may not throw an exception of type exception or of one of the standard exception types used for language support. Where in the Standard am I forbidden from "throw...
2
by: TS | last post by:
i'm wondering if it is preferred practice to throw exception in this circumstance. I have seen it done like that, but i have also read that you should try to never throw an exception in...
1
by: z. f. | last post by:
in vb asp.net page i'm overriding the finalize method in order to make cleanup. if i throw exception there it is not seen on the page. probably because the page has already sent to the client. is...
2
by: Daniel | last post by:
exception inside lock before lock body lock(foo()){bar();} what will happen if foo() throws an exception? will there be a lock on the exception?
3
by: Ryan Liu | last post by:
Hi, In the .NET Framework SDK documentation, I can see DataRow.AcceptChanges method will throw RowNotInTableException exeception. And in DataTable.AcceptChanges(), the documentation does not...
1
by: =?Utf-8?B?TVIgRQ==?= | last post by:
This may seem like a stupid question but in C#: Say for instance I have a set of SQL processes that I run via ExecuteReader(). These processes return several pieces of information to the...
4
by: George2 | last post by:
Hello everyone, In Bjarne's book, it is mentioned that sort of STL may throw exception, like sorting elements in a vector. In what situation will sort throw exception? I can not find a case....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.