F:\Java\Concepts\Threads\source files>java ThreadsWithManyLocks
1 Thread1
2 Thread1
3 Thread1
4 Thread1
5 Thread1
1 Thread3 // is this because of the reason mentioned above?
2 Thread3 // it gets executed before t2
3 Thread3
4 Thread3
5 Thread3
1 Thread2
2 Thread2
3 Thread2
4 Thread2
5 Thread2
F:\Java\Concepts\Threads\source files>java ThreadsWithManyLocks
1 Thread1
2 Thread1
3 Thread1
main0
4 Thread1
main1
5 Thread1
main2
main3
main4
main5
1 Thread2
2 Thread2
3 Thread2
4 Thread2
5 Thread2
1 Thread3
2 Thread3
3 Thread3
4 Thread3
5 Thread3
SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Prasad Kharkar wrote:3. According to my knowledge, when we write synchronised(t2) then the actually running thread i.e. main thread acquires the lock of the object t2 and no one thread of object t2 can access the synchronized code till the execution of thread is completed,
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
1 Thread1
2 Thread1
3 Thread1
4 Thread1
5 Thread1
t3 will start after this synchronized block is over
t2 will start after this synchronized block is over
1 Thread2
2 Thread2
3 Thread2
4 Thread2
5 Thread2
1 Thread3
2 Thread3
3 Thread3
4 Thread3
5 Thread3
1 Thread1
2 Thread1
3 Thread1
4 Thread1
5 Thread1
t3 will start after this synchronized block is over
t2 will start after this synchronized block is over
1 Thread3
2 Thread3
3 Thread3
4 Thread3
5 Thread3
1 Thread2
2 Thread2
3 Thread2
4 Thread2
5 Thread2
SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Prasad Kharkar wrote:I have inferred from the program as follows
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Ankit Garg wrote:In the above code, the Thread that will call the run method will be t2, but the run method will actually be called on obj2. When we synchronize on t2 in main method, the call to synchronized run method will not be blocked. But when we synchronize on obj2 in main method, the call to synchronized run method will be blocked till the lock on obj2 is released by main thread. Synchronizing on t2 or t3 doesn't have any effect if the run method is synchronized...
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan wrote:My doubt, By synchronizing the Thread object, what we can do? Are there any methods/synchronized methods to be invoked on top of the Threads(t1,t2,t3). I think, there is a only one method, start() which is invoked on top of Thread(t1,t2,t3) (but the run() method also there, most of the time this(run()) method is invoked on targets not on the threads itself), but it is not synchronized. So no need of synchronizing the Thread(t1,t2,t3) object?
Am I correct? Please confirm! I'm expecting the explanation from Ankit Garg....
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
That's a very big dog. I think I want to go home now and hug this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|