• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
shashank dwivedi
Ranch Hand
Posts: 65
AngularJS Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shashank dwivedi wrote:
why priority of one thread is 2 and other is 4


Thread ID is getting printed here, not the priority.
I did below to get the priority printed


And got below-
| 7 > 5 | 7 > 5 | 8 > 5 | 8 > 5


Priority of both the threads is default since the code is not changing it.
And I think
4422 or 4242
depends on the machine(jvm and OS).

You'll definitely not get
4444
since these are 2 diffrent threads and will always always unique IDs.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shashank dwivedi wrote:


first of all as Anirudh said we are not talking about priority here. what your code is printing is Thread Id. now coming to your question. here we have 2 threads running concurrently. let me put this way, you have started 2 threads , call them Worker A and Worker B . now these two workers are executing the same code of 2 different objects. In your code you have created 2 chess objects and have passed one to each thread worker. so Worker A will run its own copy of run method of say first chess object and Worker B will have its own copy of run method of second(different) chess object. now when worker A enters move method it acquires lock on first chess object. this won't affect other thread because other thread acquires lock on completely different chess object. so you have 2 threads running concurrently. which means output of 4 2 42 is possible. you can also get 4422 or 2244 or 2424.
 
shashank dwivedi
Ranch Hand
Posts: 65
AngularJS Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:
shashank dwivedi wrote:


first of all as Anirudh said we are not talking about priority here. what your code is printing is Thread Id. now coming to your question. here we have 2 threads running concurrently. let me put this way, you have started 2 threads , call them Worker A and Worker B . now these two workers are executing the same code of 2 different objects. In your code you have created 2 chess objects and have passed one to each thread worker. so Worker A will run its own copy of run method of say first chess object and Worker B will have its own copy of run method of second(different) chess object. now when worker A enters move method it acquires lock on first chess object. this won't affect other thread because other thread acquires lock on completely different chess object. so you have 2 threads running concurrently. which means output of 4 2 42 is possible. you can also get 4422 or 2244 or 2424.


Thanks all. I GOT IT.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic