Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

synchronized question  RSS feed

 
Markus Schmider
Ranch Hand
Posts: 147
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This example is taken from the Oreilly "Advanced Java" course.



When I run this the demoCouter() method consistently returns different values <1000. That is expected.
However the demoSyncCounter() shows the same behavior.
That puzzles me.

A typical output is
Counter count=980
SyncCounter count=983


What is most amazing, is that the guy in the video also gets a wrong result in the first execution and simply ignores it, in the second run he gets 1000 "as expected"

 
Tobias Bachert
Ranch Hand
Posts: 59
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The synchronized version does not await the termination of the executor service and thus the main-thread might call getCount prior all threads have finished their task.
 
Norm Radder
Rancher
Posts: 1733
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the call to service.shutdown() keep some threads that are waiting to execute from executing?
Put a Thread.sleep() before it to see what happens if it is delayed.
 
Stephan van Hulst
Saloon Keeper
Posts: 6980
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing to do with threads being killed or with locks.

ExecutorService.shutdown() returns immediately, meaning getCount() is going to get called as soon as the method is done submitting tasks. Solve this by using ExecutorService.awaitTermination().

You should always put ExecutorService.shutdown() in a finally-clause. You can also combine the submission of the two different types of tasks in one loop:


This should demonstrate that syncCounter will always count to 999, while counter may count to lower numbers.
 
Markus Schmider
Ranch Hand
Posts: 147
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answers.
Putting in a Thread.sleep() resolves the issues.
But surely this cannot be the correct way to use the ExecutorService


shutdown()
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.


My explanation is that the main thread calls the getCount Method before all threads have run.

With


the code works as expected: always returning 1000;

That leaves the question how well the author of this video course researched the topics he presents.
 
Stephan van Hulst
Saloon Keeper
Posts: 6980
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code you've written there, you should perform awaitTermination() after shutdown(), and shutdown() should be in a finally-clause.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!