Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

NewSingleThreadExecutor  RSS feed

 
Florian Jedamzik
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!