• Post Reply Bookmark Topic Watch Topic
  • New Topic

question about passing an Object to another thread

 
Robert Strong
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, I'm new in multi-threading, I know that to share data among threads, synchronization must be employed.
But in the books, I've seen lots of code like below:


public void read(final Connection con){
executor.submit(new Runnable() {
public void run() {
con.process();
}
}
);
}

I think here the Object con is passed from current thread to another thread in the executor pool. But no synchronization is used besides the final modifier, would it still work?

 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You only have to synchronize if there is any chance that two threads would access the same data at overlapping times. A "lot of code" may be in examples, that show you how to do things like use an object instance in an anonymous inner class without bothering about whether access to that data needs to be synchronized.

Yes it will work. If there are overlapping data accesses, then that may cause a problem.

rc
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way you say that, it sounds like synchronization is like holy water, just sprinkle it around and things will be all right. The reality is more complex. With the code you posted, there's no evidence that anything is ever going to be shared among threads. And for another thing, you're using a variable named "executor"... one might guess this is an instance of java.util.concurrent.Executor, which I'm sure takes care of synchronizing processes which it knows have to be synchronized. Of course that doesn't mean that you don't need to synchronize some of the code which you posted, either. It just means that you can't tell anything from that fragment.

As for the final modifier, that doesn't have anything to do with threads or synchronization. All it does (officially) is to prevent any code from assigning anything to that variable, and indeed there isn't any code which does that. In practice the modifier just has to be present to allow the variable to be used in the inner class. However it doesn't prevent any code from modifying the object to which the variable refers; that might indeed happen during the call to con.process(), which would be completely normal.

 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Picky Mode)
You do NOT "pass an object" you pass a reference. Using the words "pass an object" creates the impression that the object is now totally under control in the method you "passed" it to. Reality is there could be any number of Threads having references to that object - thats where the programmer has to exercise some thought.
(/Picky Mode)

Yours for more precise language and thinking,
Bill
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First synchronization does TWO things i) it can prevent concurrent modification AND ii) it provides happens before ordering, ensuring if used correctly the writes of one thread are visible to another.

Final does (being picky here I know) matter to threads see the Java Memory model just not in this context (i.e. initialisation of member variables have some guarantees on visibility).

This example has quite a lot of complexity too it, one is how you determine the threads aren't writing at the same time to the same object, the second that how do you determine that any writes are actually visible from one thread to another i.e. thread A sets true but B still sees false stuff.

As has already been said final is only protecting the reference so the referenced members are mutable which might cause problems BUT is the implementation of connection itself thread safe ie are all its members synchtronized ?

Also note there is almost certainly another form of synchronization present i.e. think of how the job gets queued to the executor, this is important as two threads synchronizing on the same object establish happens before ordering and allow visibility. So saying this example doesn't use synchronization isn't necessarily true at all sorts of levels. (I'm using synchonization in its weakest form i.e. not necessarily the synchronized keyword)
 
Nidhi Singhal
Ranch Hand
Posts: 89
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[color=blue][/color]

Here, a new Runnable() is being passed to the executor.submit method so when the submit is called, the run() of the passed Runnable() will be executed which in turn will call con.process(). So actually a new thread is being started which executes the process() method of the Connection (con) object. And since con is final so con.process() cannot make any modifications to the Connection object. Synchronization is generally required to avoid data corruption (i.e maintain the data integrity). Here 'con' being final cannot cause data corruption hence it is not called from within a 'synchronized' context.

Thanks,
Nidhi
 
prem pillai
Ranch Hand
Posts: 87
Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nidhi Singhal wrote:And since con is final so con.process() cannot make any modifications to the Connection object

Con being final doens't stop , its internal state getting changed. Read what Ralh , Paul & Chris said.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!