Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

synchronized question  RSS feed

 
Ranch Hand
Posts: 153
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"

 
Ranch Hand
Posts: 86
18
  • 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.
 
Rancher
Posts: 3353
33
  • 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.
 
Saloon Keeper
Posts: 9989
207
  • 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: 153
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: 9989
207
  • 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.
 
Seriously Rick? Seriously? You might as well just read this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!