File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads" Watch "Threads" New topic
Author

Threads

shashank dwivedi
Ranch Hand

Joined: Mar 06, 2012
Posts: 57
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

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.


Anrd
"One of the best things you could do is to simplify a larger application into a smaller one by reducing its process and complexity - Fowler"
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

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.


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
shashank dwivedi
Ranch Hand

Joined: Mar 06, 2012
Posts: 57
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Threads
 
Similar Threads
Synchronization and Locks
K and B Chapter 9 self test qn no 17
[Thread] Synchronized - Object Locks
Synchronization
synchronization: whats the answer