This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes synchronized/wait(), How to use them in my case Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "synchronized/wait(), How to use them in my case" Watch "synchronized/wait(), How to use them in my case" New topic
Author

synchronized/wait(), How to use them in my case

Reyada Wolak
Greenhorn

Joined: Mar 28, 2009
Posts: 11
Hi,

I'm trying to use wait() in a synchronized method in my application. on the other hand it seems 'synchronized' is not blocking other threads to access it. I prepared the a sample code to test it:

Q.java
******


S.java
******

**********

if you run S.java and start pressing the button, you will see:
start
start
start
start
start

That means the method is not exit and another thread start execute it again. If you replace wait() with Thread.sleep() it works as expected:
start
end
start
end
start
end

On the other hand i need to use wait() and not sleep() in my case.

I appreciate any help

thanks



K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2247
    
    7

what is the Q class waiting for? And the JFrame is Q class is really pointless - just makes thing annoying.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Reyada Wolak wrote:if you run S.java and start pressing the button, you will see:
start
start
start
start
start

That means the method is not exit and another thread start execute it again.


That is correct. When a Thread wait()s on an object, it releases the lock on the object so that other threads can gain the lock. Why is this important? The only normal way out of a wait() is through the notify(). notify() must be called from another thread, and can only be called inside a synchronized block. Since you never call notify() none of your threads come out of the wait() - and you have what is called deadlock - which will eventually eat resources as you will generate a bunch of threads that can't end.

If you replace wait() with Thread.sleep() it works as expected:
start
end
start
end
start
end

On the other hand i need to use wait() and not sleep() in my case.

Why do you need to use wait()?


Steve
Reyada Wolak
Greenhorn

Joined: Mar 28, 2009
Posts: 11
K. Tsang wrote:And the JFrame is Q class is really pointless - just makes thing annoying.


Sorry, I was only trying to put the same code as in my application.

K. Tsang wrote:what is the Q class waiting for?

Steve Luke wrote:Why do you need to use wait()?

I'm developing an application that reads the logs from the output (shell) of a native application. I'm waiting until specific line is displayed (output) and then I use a notify() to release the synchronized function.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Sounds like you might be using the wrong paradigm. Instead of using a bunch of runnable threads that have to wait for a specific line, you might think about using an event-based model.

1) Generate a Blocking Queue your producer (whoever would have been reading the output and notify()ing the waiting thread) can reach. When it gets to the point where it needs to trigger the task (the output you are looking for is reached), it adds an 'Event' to the Queue.

2) Generate a single Thread ('Event Thread') which listens to the Blocking Queue for an event. When an event comes into the Queue it dispatches it to a 'Task' or 'Listener'.

3) The 'Task' or 'Listener' does the work for that particular event.

This way your tasks are run in the same Thread - no synchronization issues, and one can't start until the previous finishes - just like you want. It simplifies the triggering, reduces the number of overall threads, and scales pretty well to many events/event types and listeners/tasks.

If that sounds familiar, it should, it is how the AWT/Swing 'Event-Dispatch-Thread' works, and is a good model for waiting for specific events to occur.
Reyada Wolak
Greenhorn

Joined: Mar 28, 2009
Posts: 11
Thanks Steve for your help.

I tried to find a way to implement your suggestion without success. I don't know a lot about 'Event-Dispatch-Thread'. I would really appreciate if you give me an example or a link to a tutorial on how to do this.

Actually my application is trying to control MPlayer (in slave mode) through its output stream and getting the answers from the input stream. I'm using a base code from:
http://beradrian.wordpress.com/2008/01/30/jmplayer/

On the other hand, I faced a lot of problems in using PipedInputStream/PipedOutputStream since they are *sometimes* start to consume a lot of CPU in idle cases. So I remove them and use wait()/notify() to replace the waitForAnswer() function.

I couldn't make it works.
I appreciate your comments
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2247
    
    7

So you are using JMPlayer code base and modifying it a bit or something? Or writing like a plug-in to control slave mode part for your own use?

Seem I can find java-base apps everywhere: Vuze and now JMPlayer. yippee.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Sorry for the late reply. Yes I do have an example. This is a pretty simplified version but it should get the point across, I hope:

First, this is an interface for the type(s) of events to handle


Another interface to handle tasks to be done when the event occurs:


The 'meat' of the example is an 'EventConsumer' which
1) has an Event Consumer Thread that will execute all the Tasks in
2) has methods to trigger an Event from an external source
3) has methods to register Tasks to do in case of an Event.



Now time to test it. A simple task to do (QTask) which pretends to work


And a GUI that has a button to trigger the tasks (kinda silly I know, but just a sample on usage)
Reyada Wolak
Greenhorn

Joined: Mar 28, 2009
Posts: 11
Thanks a lot Steve. your examples helps me to understand the idea. Also I came to know a new class BlockingQueue which does a good job for me.
I'm trying to modify my code accordingly.

Thanks a gain.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronized/wait(), How to use them in my case
 
Similar Threads
Unable to understand threads
Two JDialogs with the same parent
multithreading not working correctly
Threads and ActionListeners
Synchronizing between two threads, one implements Runnable the other extends Thread