• 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

Threads

 
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Foreman
Posts: 275
jQuery Eclipse IDE Spring
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic