Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Trying to print odd and even by threads

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone please suggest me why my code is not working as expected.




Here I have taken two threads T1 and T2 for printing odd and even simultaneously
 
Ranch Hand
Posts: 261
15
Android Angular Framework Spring AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, AtSvm (Mr. Garg):

I can tell you one thing that I see which is not as intended, and another kind of broader suggestion.  

Note the new setting of thread status?  Well, by default when you declare a boolean field (class data member) in java, it will be set false.  So when your code enters that loop and tests for the first time whether the value matches, it never will for the second thread.  I added the commented line to cause the turn status to be different for the second thread (which is set to the opposite boolean value).  This causes both threads to actually go forward.  And it is because there is no way for that boolean variable to transition (see line 29 in your code) that the second loop goes into an endless loop and never exits.

Now the other observation.  This code looks like it is trying to solve one problem two different ways.  On the one hand you have two different threads, and on the other you have this if-check within the while block to switch one thread on or off.  Even after I made this change I don't see the desired behavior. There are outputs from each thread interspersed but they are not in the order you wanted.  Another symptom of this split is that your synchronization variable is separate for each thread.   If you make a "new Turn()" outside of the MyThread class you might also get better results.

I see this as a nice exercise; I would not be trying to force two threads to synchronize in this fashion.  If I were, I might instead establish some kind of "round robin scheduling" approach.  For that, there would be a shared monitor object (like Turn) that would just have each thread waiting for it with a "notify" like you have there.  You could also lookup "Round Robin" and see if it leads you back to the Java concurrency package. I've never bothered to look.

Give that a try.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic