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 7562 JosAH 11,448
Recognized Expert MVP
Which Exception has been thrown? Can you produce a stack trace?
kind regards,
Jos
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
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:
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()
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
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 exception and throw it.
I know how to do it if we hardcode the exception
|
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
|
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
|
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...
|
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.
| |
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 mention it will
throw any exception, but in my code (multi-thread), I see it throw
exceptions at two situations:
dr = this.currentQuotaUserDt.NewRow();
|
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...
|
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,
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |