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 ) -
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.
5 7526
Which Exception has been thrown? Can you produce a stack trace?
kind regards,
Jos
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
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:
instead of: - client.wait();
-
client.notify();
kind regards,
Jos
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()
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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?
|
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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
|
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...
| |