Skip to main content

Java wait/notify

1 reply [Last post]
ingvar_e
Offline
Joined: 2012-04-02
Points: 0

Working on a Blackberry project. Its divided into 2 different GUI:s, 1 background monitor and several "action"-threads. All communicating with each other. I guess I have jumped a bit to conclusions when implementing a rather complex wait/notify scheme for the app. The symptom I get is that notifies "get lost".
My conclusion is that notifies dont get queued and I would like to verify that that is the case. What happens if two notifies are fired simultanously at the same wait object? Both are synchronized on the wait object so they will follow each other "instantly"
Or are there other instances where notifies "get lost".

I really dont feel like designing some queueing mechanism for this.

Advice is appreciated

Ingvar i Las Palmas

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ingvar_e
Offline
Joined: 2012-04-02
Points: 0

Well I found out.
I am using "Vector" as both the wait/notify object and as a communication vehicle for data passed to the thread and data communicated back. Excellent tool. I have a Sting array and an int array as components of the vector. a test like below illustrates what happened:

NotifyStringV = (String[]) V_TestVector.elementAt(0); Get array of string initiated ""

HandleAlert(_Setup.Debug,"NotifyStringV[14]: "+NotifyStringV[14],0); show that indx 14 is empty

NotifyStringV[14] = "kalle"; Set a value at index 14

NotifyStringV = (String[]) V_TestVector.elementAt(0); Read from the vector again

HandleAlert(_Setup.Debug,"NotifyStringV[14]: "+NotifyStringV[14],0); show what indx 14 contains

Surprise!

NotifyStringV[14]: HandleAlert 1
NotifyStringV[14]: kalle HandleAlert 2

Retriving an array, updating a value in the array actually changes the value of the arry "inside" the Vector.

So "Vector" is not a "container" in the sense I think of it that has to be retrieved and replaced.
That is what I thought when I started to code the routines for wait/notify.

That resulted in me actually changing the values of the arrays in the Vector outside of synchronized
and just using synchronized when doing a replace of the array into the vector.

I did some reading up on vectors but this way of functioning was not obvious.
I just hope its a "feature" and not a "bug" :-)