File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Inter thread communication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Inter thread communication" Watch "Inter thread communication" New topic
Author

Inter thread communication

Amit Juneja
Greenhorn

Joined: Feb 07, 2012
Posts: 3

Again it is very basic producer-consumer problem. Here I am not getting expected output. I was thinking to get the following output

Fresh order has been taken by Thread-0
Order has been Saved by Thread 1 Order taken-1
Ready to take the fresh order again Thread-0

Fresh order has been taken by Thread-0
Order has been Saved by Thread 1 Order taken-2
Ready to take the fresh order again Thread-0

Fresh order has been taken by Thread-0
Order has been Saved by Thread 1 Order taken-3
Ready to take the fresh order again Thread-0


The piece of code that I am using consist of two classes operator 1 and operator 2



and




The output I am getting with this code is just onordered

Fresh order has been taken by Thread-0
Order has been Saved by Thread 1 Total Order taken-1
Order has been Saved by Thread 1 Total Order taken-2
Order has been Saved by Thread 1 Total Order taken-3
Ready to take the fresh order again Thread-0
Fresh order has been taken by Thread-0
Ready to take the fresh order again Thread-0
Fresh order has been taken by Thread-0


So just want to know the reason of this output and how to get the desired output.
Thanks in advance.
Krishna Kumar S
Greenhorn

Joined: Jan 31, 2012
Posts: 12
What exactly is your problem statement??
The Operator1 extends thread but has not overridden the run method. so why to extend the Thread class.
For inter thread communication objects are used between threads. (to wait/ notify on that object you need to acquire the lock of it).
But why you are waiting on thread t2. Note: Thread wait on shared objects for synchronization.


Example: Stock objects, Stock Analyzer thread , Stock Modifier thread. Stock Analyzer thread wait on Stock object and will get notification when Stock Modifier thread updates it.
Amit Juneja
Greenhorn

Joined: Feb 07, 2012
Posts: 3

1. if Operator1 does not override run method here it does not make any difference.

2. t2 object's lock is used here for the inter thread communication between main thread and thread t2.
Krishna Kumar S
Greenhorn

Joined: Jan 31, 2012
Posts: 12
Though i do not get the usage of this program. I could explain the output of your program.
Once notify() is executed in Operator2 , operator 1 will be taken from wait state and placed into "ready to run" state.
But it doesn't guarantee that it get the CPU as soon as it is notified. so there are still chances that Operator2 gets the CPU and get the lock of this(operator2).
If you want both should execute one after the other, both should wait and notify each other.
Hope it helps!!!
Amit Juneja
Greenhorn

Joined: Feb 07, 2012
Posts: 3

Thats what I want that they should execute one after the other. But it is not working like this. What all changes should I do here ? The desired output is mentioned in the first post...

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Amit Juneja wrote:Thats what I want that they should execute one after the other.


Then don't use threads. The whole point of multithreading is that multiple tasks can execute in parallel and independently of each other. If I have threads T1 and T2 each executing steps S1, S2, S3, and logging each step along the way, any of the following would be valid:



and so on.

A typical producer/consumer situation does NOT operate as


While that can be the case, it's much more common for P and C to both be working at the same time. There's usually a queue between them because sometimes P will be producing faster than C is consuming.

At any point, one may have to pause and wait for the other one. P may have to pause if the queue gets full, waiting for C to remove something, and C will have to pause if the queue gets empty, waiting for P to produce something. At any other time, though, both can be working simultaneously, or, if there's a single CPU, there can be any combination of smaller steps by one until the CPU gives time to the other one.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inter thread communication