aspose file tools*
The moose likes Threads and Synchronization and the fly likes Executor.execute( Runnable ) thread safety Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Executor.execute( Runnable ) thread safety" Watch "Executor.execute( Runnable ) thread safety" New topic
Author

Executor.execute( Runnable ) thread safety

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

Is this method thread safe ? Pardon me if I'm asking a stupid question.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

Executor is an interface. So you are asking a question about all of the possible classes that might ever implement it. Besides, for each of them the question mostly boils down to whether the Runnable being executed is thread-safe. So I would say the answer is "Don't count on it."
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

Paul,

Assuming that all my Runnable(s) are thread-safe, then I gather from your answer that I will still need to synchronized access to invocations of Executor.execute( Runnable ). Thanks.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Originally posted by Pho Tek:
Paul,

Assuming that all my Runnable(s) are thread-safe, then I gather from your answer that I will still need to synchronized access to invocations of Executor.execute( Runnable ). Thanks.


Executor.execute( Runnable ) is not a blocking call. It will execute your task sometime later. So, synchronized access to its invocation would not help.
I think if your Runnable instances are using shared resources and they are thread-safe then you do not need to worry about anything in the world but if they are not thread-safe and they are accessing shared resources then you should make them thread-safe. There is no middle-path


apigee, a better way to API!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Executor.execute( Runnable ) thread safety