473,796 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

14 New Member
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 7562
JosAH
11,448 Recognized Expert MVP
Which Exception has been thrown? Can you produce a stack trace?

kind regards,

Jos
Apr 3 '07 #2
cozsmin
14 New Member
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
Apr 3 '07 #3
JosAH
11,448 Recognized Expert MVP
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 New Member
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 Recognized Expert MVP
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
5156
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 exception and throw it. I know how to do it if we hardcode the exception
3
458
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 Exception and Throw
2
1578
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 exception();"? Thanks, Dave
2
2541
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 circumstances where you can do some test before the operation that would throw the test so that you can by pass the exception handling which is an expensive operation. So lets say the scenario is i have an object that is validating itself in the business...
1
1352
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 there a way to throw exception on the finalize method in order to check that objects were closed, but how do i trace this exception? TIA, z.
2
1584
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
14800
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 mention it will throw any exception, but in my code (multi-thread), I see it throw exceptions at two situations: dr = this.currentQuotaUserDt.NewRow();
1
1778
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 DataReader, including any SQL exception that occurred (This may include messages I generated b/c something didn’t check out based on programmed requirements, but its not a runtime exception as far as SQL or C# is concerned). I then check this...
4
2236
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. thanks in advance,
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9525
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10452
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.