Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Executor.execute( Runnable ) thread safety

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this method thread safe ? Pardon me if I'm asking a stupid question.
 
Paul Clapham
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic