• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

synchronized question

 
Ranch Hand
Posts: 153
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15491
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15491
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Everyone is a villain in someone else's story. Especially this devious tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic