• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two Threads with priorities

 
Alexandru Gifei
Ranch Hand
Posts: 42
Java Linux Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,
I'm having an issue with 2 threads that run with different priorities. The problem is that the program prints the 2 counters, but does not stop. YThread is similar to XThread. Thanks

 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if that's the problem , but you may want to declare isRunning as volatile - indicating its value can be altered in different threads.

From what I remember - you shouldn't rely on thread priorities for doing anything meaningful. Their implementation is (or used to be) very uneven and sometimes changed between JVM versions, so it is better not to rely on them. Maybe Henry will chime in on this, he'd know for sure.
 
Alexandru Gifei
Ranch Hand
Posts: 42
Java Linux Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declaring the flags as volatile did the job. Thanks!
 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alexandru Gifei wrote:Declaring the flags as volatile did the job. Thanks!


True. I guess when the count is not volatile, the 2 threads tries to do code optimization by reading/writing the variable to cache/master location. Hence the 2 thread enter race condition and will not terminate.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic