• Post Reply Bookmark Topic Watch Topic
  • New Topic

TestThreads class [code mangets-Networking and threads-ch15-HeadFirstJava]  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi anyone and everyone!
I'm really a beginner, and try to complete the really good book Head First Java, which has an exercice called Code magnet and is all about threads.
I found a post about this exercice, but not about this specific problem stated below.
The output to find is :
one 98098
two 98099

I succeeded in my own way to get an output, and it's robust. I don't even really understand why.
When I implemented the exercice solution although, I couldn't match the output that I quoted.

Down there is my solution,I cheated using two times the staments a.updateCounter(1);  and the comments in the code are part of the exercice solutions as stated in the book (assuming one removed the duplicated statement).

My question, am I really multithreading here?
Because, as far as my eyes can see, I'm putting the second thread to sleep while the first is working, and when the first finishes, the second wake up and use the updated value.
I don't even need the first thread to sleep.
I understand why the second thread has to sleep, (otherwise it finishes way before the first like "two 1 one 98098"), and I understand the design Singleton.
But here, I'm trying to monitor the scheduller, which seems not to do, right? to achieve a result I could get without threading at all.


 
Henry de Deaux
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, GOT IT!  

Singleton design was the point of the exercice and threading the context.
You can use some timer working with the scheduler, even if no guarantee is due, as stated in the book.
I just freaked out a bit     
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!