• 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

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

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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      
reply
    Bookmark Topic Watch Topic
  • New Topic