aspose file tools*
The moose likes Threads and Synchronization and the fly likes Multithreaded Example... Where to put WAIT() , NOTIFY() advise please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Multithreaded Example... Where to put WAIT() , NOTIFY() advise please " Watch "Multithreaded Example... Where to put WAIT() , NOTIFY() advise please " New topic
Author

Multithreaded Example... Where to put WAIT() , NOTIFY() advise please

Ronald Vermeij
Ranch Hand

Joined: Sep 05, 2009
Posts: 37
Hello All,
Lately I've have been busy studying Java Multi-threading mechanismes like mutexes, synchronized blocks, shared objects, locks, object monitor. I only have a little (grrr) challenge with fitting in the Wait. Notify and Notify_all into my multi-threaded code.
I have dug deep into the "JVM internals" to see and really understand what is happening at the low-level between the JVM, the JVM Schedular, the JVM object monitor and the separate Java objects that want to communicate with eachother and share / exchange data.

I have created the following example:
- 1 Launcher thread.. that fires off the rest of the thread and is able to control them from the outside (start, pauze, stop)
- 3 Message-producer thread... that generates and hand of messages to the ..
- 1 Message printing thread .. this is shared by all 3 Message producing threads
Please see the code(s) below..

1. Launcher


2. Single thread (message producing)


3. Shared thread (message consuming and printing)



Everything works just fine right now WITHOUT! the usages of WAIT() and NOTIFY() java statements.
- So far I've already figured out (from tons of books and online examples) that the "shared thread or object" / "locked object" is the place where I can put the NOTIFY() statement after an operation on that object has been completed successfully.

- I still have a hard time figuring out how and where to put the WAIT statements in the right spot


Now my question to you "multi-threaded guru's"
Can you please help me by explaining WHERE and HOW I can use the:
- WAIT in the 2nd code listing?.....................the thread that want to use another shared object
- NOTIFY (NOTIFY_ALL) in the 3rd listing?..... the shared thread that is used by the single threads
(eventually by re-writing some of its parts??)

Thank you very much for your help, feedback and (code)examples

regards, Ronald



Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Hi Ronald. I've studied your code a bit and I'm wondering about your ultimate
objective using this code. Are you just trying to understand how to coordinate
producer and consumer threads, or do you have something else in mind?

Jim ... ...


BEE MBA PMP SCJP-6
Ronald Vermeij
Ranch Hand

Joined: Sep 05, 2009
Posts: 37
Hey Jim,
Thank you very much for taking the time and effort to study my listings!

To answer your question:
1. Yes I am trying to understand how I can create multi-threaded code.
The purpose of listing 1 and 2 is to investigate how I can created "single stand-alone" threads,
- that can be "launched" by a "mother program" (the launcher)
- which will run "for ever", once started
- which can be "remotely controlled" (started, pauzed and terminated) from outside the thread itself.

The purpose of listing 2 and 3 is to investigate how I can created
- "multiple producers" vrs a "single consumer" thread
- inter-thread communication mechanisms like..
-> How to check if the (other) thread alive?
-> How to check if the (other) thread is accessible for calling threads?
-> How the (other) thread can tell his environment that he is busy / free?
-> When to use an (every lasting) THREAD as consumer? vrs
-> When to use a (dead) OBJECT as consumer?


2. I also want to get "a good feeling. insight" with multi-threading programming on how and when to use the
various multithreaded communication and synchronization mechanisms like:
- (wait, notify) -------------------------------------------------------> producer, consumer scenario
- (wait, notify_all) ----------------------------------------------------> producer, consumer
- calling synchronized {blocks of code}----------------------------> multiple threads with 1 (or more) shared object
- calling {entire methods} from one thread to another thread -->multiple threads with 1 (or more) shared object
in what kind of different scenarios.

I have read (and played with) lots of code examples from books like:
- Concurrency examples" - Sun Microsystem own tutorial
- "Concurrency: State Models & Java Programs" - J Kramer, J Magee (the best book for me so far!!)
- "Java threads 2nd edition" - O Reilly
- "Multi-threaded programming with java technology" - Sun Press
- "Java thread programming" - Sams
- "Java Concurrency in Practice" - Brian Goetz
- "Inside the Java Virtual machine" - Bill Venners
- "Thinking in Java" - Bruce Eckel
And numerous online tutorials, downloadable code examples and education materials i could find like
Jakob Jenkov - Java Concurrency tutorials - http://tutorials.jenkov.com/java-concurrency/index.html

As you can see i have used all kind of different sources on Java multi-threading technologies, to from a clear picture on when to use what technology in which situation...

The reason I asked my question is that i have a "gut feeling" that i can solve this current challenge ALSO with Wait, Notify (since it is a producer, consumer scenario,pattern) right... but i have not exactly figured out how.... and that is why i need a little help in modifing listing 2 (for the wait) and 3 (fro the notify, notify all) statement to be injected
Garik Ustinov
Ranch Hand

Joined: Jun 22, 2009
Posts: 31
You have 4 Threads working together without a single synchronized block, concurrency api usage or even volatile variable. I guess it just cannot be right. Take a closer look, maybe there's something wrong with it.
There are plenty of Producer-consumer examples java producer consumer available. I suggest you to start with some simpler examples, rather than digging in Java Concurrency in Practice.


SCJP, SCWCD
Ronald Vermeij
Ranch Hand

Joined: Sep 05, 2009
Posts: 37
Garik Ustinov wrote:You have 4 Threads working together without a single synchronized block, concurrency api usage or even volatile variable. I guess it just cannot be right. Take a closer look, maybe there's something wrong with it.

Thanks Garik for pointing this out. This is how it should be done in relation to synchronized block(see code fragments)
Listing 2 and 3
Listing 3

Garik Ustinov wrote:There are plenty of Producer-consumer examples java producer consumer available.
I suggest you to start with some simpler examples, rather than digging in Java Concurrency in Practice.


Thanks to your hint..I have found some very beautiful, visually supportive examples online.
I can reccomend them to anyone who is learning Java Multi-threading!

George Mason University - department of computer science
1. Producer_Consumer - Semaphore based:
http://www.cs.gmu.edu/cne/workbenches/pcsem/pcsem.html
http://www.cs.gmu.edu/cne/workbenches/pcsem/Semaphore.html

2. Producer_Consumer - Monitor based:
http://www.cs.gmu.edu/cne/workbenches/pcmon/pcmon.html
http://www.cs.gmu.edu/cne/workbenches/pcmon/monitor.html

3. Producer_Consumer - Message based:
http://www.cs.gmu.edu/cne/workbenches/pcmsg/pcmsg.html
http://www.cs.gmu.edu/cne/workbenches/pcmsg/Message.html

Website of Rafael Stekolshchik:
http://cities.lk.net/approco.html


Thanks for your feedback Garik
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Ronald : Can you explain what is happening in the code below between the
producer and consumer threads? The code uses the wait() and notify() tools
of the java API with 'queue' as the lock object. Jim ... ...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

Jim Hoglund wrote:Ronald : Can you explain what is happening in the code below between the
producer and consumer threads? The code uses the wait() and notify() tools
of the java API with 'queue' as the lock object.


As an FYI... You actually don't need to use wait and notify, with a queue. You can achieve the same functionality by using a BlockingQueue instead.


But to answer the question... Basically, every time a producer puts something in the queue, it will send a notification. On the consumer side, it will take items from the queue, but if none is available, then it will call wait() instead.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ronald Vermeij
Ranch Hand

Joined: Sep 05, 2009
Posts: 37
Ronald : Can you explain what is happening in the code below between the producer and consumer threads? The code uses the wait() and notify() tools of the java API with 'queue' as the lock object.

- 1 Queue object is created (in the form of a LinkedList object)
- 3 consumerthreads are created, initialized and started
- 3 producerthreads are created, initialized and started
- All 6 threads "are connected" / "communicate" via 1 and the same "queue" object.

Producer thread steps (in plain English)
- has a "self-ending" threadloop after 10 cycles (for..loop)
- generates a random number
- locks the queue object (to get single acces to it), via the synchronized mechanism
so one 1 (producer or consumer) thread at the time can have access to the queue object.
- "offers" the random number (num) to be placed in the queue object via queue.offer method.
- The value inside (num) is being placed into the LinkedList.
- is ready with its operation on the queue object,
- releases the "object lock" from the queue object,
- notifies this good news (queue object is free again :-) ! )
to the other threads who want to have access to the queue object
- prints out the random number
- goes to sleep for a while.
and repeat these steps above 10 times.


Consumer run() thread steps (in plain English)
- has an "ever lasting" threadloop .. while (true)

- locks the queue object for single access via the synchronized method.
- "looks in the queue", if their is any data present via queue.peek method
- If queue is empty,
... the consumer thread is put in "wait mode".. until the producerthread "notifies" the consumerthread
... that it has put some data into the queue. In 'wait mode" queue objectlock is released , so other threads
... can try to lock the queue object for themselves.

- When consumer thread has found data in the queue,
... it retrieves the first data element from the queue via "queue.pol" method

- This data from the queue is then printed on the console
- These steps are repeated for every (or until press Ctrl-C on the Linux console commandline)

the funny thing on this example is.. that it differs from the average Jim.
Since must P_C examples i've seen so far the producerthread "waits" and the consumerthread "notifies"
But anyway.. it does not matter, as long as it works correctly according to Java API rules

Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Good morning Henry, and Ronald, some added comments. The notify() method must
be within the synchronized block or a IllegalMonitorStateException will be thrown. And
the notification will break the wait state (return the lock to) only one thread waiting on
the queue object, but only after the producer thread releases it. As far as who waits
and who notifies, it depends on which thread is in charge; is the consumer waiting for
something to process, or is the producer waiting for approval to run. Good discussion.

Jim ... ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multithreaded Example... Where to put WAIT() , NOTIFY() advise please