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 How to pass an object from one thread to another? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to pass an object from one thread to another?" Watch "How to pass an object from one thread to another?" New topic
Author

How to pass an object from one thread to another?

Ryan Raina
Greenhorn

Joined: Jun 16, 2010
Posts: 28
Hello everybody


I have a scenario where there is an object let's say context which needs to be passed from threadA to the the other thread say threadB..
As soon as the object is passed to threadB ...threadA returns back and starts making changes to context object ..but it might happen
that threadB hasn't done it work so the value of context would have been changed..there is a problem

My question is how can we deal with this..

One scenario I can think of passing is that I would pass a cloned object of the context rather than the context itself to the ThreadB..
But let's assume it's not a option..

Also how would syncronisation work here....I can get a lock on the context itself but it still doesn't solve the problem
because it might happen threadA will get the lock and modify the context object..


Thanks in advance.....
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
1. You don't pass objects in Java, you pass references to objects - saying it as passing objects leads to confused thinking.

2. Who is responsible for the code used in the "context" object?

Bill
Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 47

Couple of options that i can think of:

1. Do not hand over context to threadB until threadA's changes to context are complete.

2. Hand over context to threadB if it is only going to 'use' the context and not 'modify' it. If this is the case then please have a look at java.util.concurrent.locks.ReentrantReadWriteLock
Word of caution: Since threadB can use the context while threadA is modifying it, make sure that threadB's code is not dependent on those changes. Why? because they may or may not happen by the time threadB tries to use the context.



Warm Regards,
Rakesh
Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 47

If both threadA and threadB are going to 'modify' the context then one needs to understand what those modifications are. Modifications are to the context object itself or to the object's maintained by the context (its not clear as to what context we are talking about).

If modifications are to the context itself then you dont have any other option than locking it. Depending on 'context' code (William's question #2) you can use synchronized() or a lock.

If modifications are to the objects maintained by the context, then you can look into locking those objects instead of context.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to pass an object from one thread to another?
 
Similar Threads
wait and notify
Sleep and Yield state
join vs wait/notify
Thread Interaction
Concept of locks and synchronization !