• 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

NewSingleThreadExecutor

 
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi;)

As I am currently finishing my preparationfor the OCP Exam, I
am working myself through Threads (the hardest Chapter ;) ).

I don't quite understand why the Output cannot be determined
exactly here:



I thought a newSingleThreadExecutor does everything sequentially?
So why would the Output be "mixed up?"

Kind regards
Florian
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Florian Jedamzik wrote:
I thought a newSingleThreadExecutor does everything sequentially?
So why would the Output be "mixed up?"



Please explain what you mean by "mixed up"..... and also... Keep in mind that there are two threads here. The one thread in the single thread executor, and the main thread which is running the code in the main() method.

Henry
 
Florian Jedamzik
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Please explain what you mean by "mixed up"..... and also... Keep in mind that there are two threads here. The one thread in the single thread executor, and the main thread which is running the code in the main() method.

Henry



By that I mean it can sometimes output 100 100, but also
100 47 or 10 86 or 100 99 etc...

Ok, I see...then the main Method runs this Code Snippet
independently from the SingleThreadExecutor:



And that's why the ouput could be "mixed up"?
Did I get this right?;)

Kind regards and thank you for your answer;)
Florian
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Florian Jedamzik wrote:
And that's why the ouput could be "mixed up"?
Did I get this right?;)



Well, "mixed up" implies that the code did it wrong. It isn't wrong. You just didn't let it finish.  Just delay the main thread a bit, to let the executor finish, and the results should be correct.

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic