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 Thread doubt 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 "Thread doubt" Watch "Thread doubt" New topic
Author

Thread doubt

shyam kumarK
Ranch Hand

Joined: May 06, 2007
Posts: 30


can someone tell me why thread execute concurrently
the numbers will be same on same line(i=1,j=1)

What I thought was because the run method is synchronized one thread must complete its run method before another thread begins.
sample output:
Thread-0 i 1,j 1
Thread-0 i 2,j 2
Thread-0 i 3,j 3
Thread-0 i 4,j 4
Thread-0 i 5,j 5
Thread-0 i 6,j 6
Thread-0 i 7,j 7
Thread-0 i 8,j 8
Thread-1 i 1,j 1
Thread-1 i 2,j 2
Thread-1 i 3,j 3
Thread-1 i 4,j 4
Thread-1 i 5,j 5
Thread-1 i 6,j 6
Thread-1 i 7,j 7
Thread-1 i 8,j 8
Thread-1 i 9,j 9
Thread-1 i 10,j 10
Thread-1 i 11,j 11
Thread-1 i 12,j 12
Thread-0 i 9,j 9
Thread-0 i 10,j 10
Thread-0 i 11,j 11
Thread-0 i 12,j 12
[ May 15, 2007: Message edited by: shyam kumarK ]

SCJP 1.4
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
On what object are you holding the lock. Are they the same object.
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

shyam


I think ,when ever a thread is in a running state,then that thread can enter into waiting state.And when a thread will enter into running state ,it depends on schedular.When ever schedular wants to start a thread ,the schedular calls run method.And you synchronized that method.I think synchronizing the run() method doesn't have any impact,i think.


Thanks
Anil Kumar
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
You created 2 objects, and it is possible to acquire lock on both. synchronize keyword used in method declaration will always try to acquire lock on (this). And since you have 2 objects, and each is acquiring lock on (this), this is always possible. Threads can run at the same time.

Take a look at code below, which is a little bit modified.
Here I create new thread, which acquires lock on itself and start printing. Immediatly after I call run from main, which is also trying to acquire lock. Important is, that it is trying to acquire lock on the same object.




To solve your problem with 2 threads, remove synchronize from method declaration and insert synchronized block to run. And synchronize on (A.class).
This ways threads will "fight" for one lock, and only thread, which acquire lock will run. Other threds must wait.
shyam kumarK
Ranch Hand

Joined: May 06, 2007
Posts: 30
Thanks John,


Anil said
I think ,when ever a thread is in a running state,then that thread can enter into waiting state.

to correct it,
when ever a thread is in a running state,then that thread can enter into Runnable state(if scheduler prefers to run another thread) and waiting state(only if it tries to enter a method or block which is synchronized on an object whose lock is owned by another thread unless you called wait on it)
i think
[ May 15, 2007: Message edited by: shyam kumarK ]
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
Here is the modified version:

John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
Originally posted by shyam kumarK:
once a thread starts run method any other thread cant enter that method because it is a synchronized method..


Any other thread trying to enter this method for _this_ object must wait.

in your first code example:
thread a1 is calling run, so it must acquire lock on object a1, OK, running
thread a2 is calling run, so it must acquire lock on object a2, OK (nobody owns lock on a2), running
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
Originally posted by shyam kumarK:
Thanks John,

I think It is wrong that the thread goes to wait....Rather it is in runnable state when the other thread runs

[ May 15, 2007: Message edited by: shyam kumarK ]

Right, I meant it as can't enter,
I'm not sure what is thread doing at that time.
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
from JLS3


I'm not sure how are they blocked. If they are in running/runnable state and are polling for that lock. Or if they go to waiting state (and are somehow notified)?
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi

I tried Jhon's code with synchronized(A.class){ and it worked. What I am not able to understand is when you acquire a lock on a class then basically you are gaurding against it's static variables and methods. When an instance of A class is used how come the threads are synchronized. They do not require a lock on A.class rather a lock on the instance of A class. Am I missing something.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
I think I got it. When synchronized(A.class) { is encountered this tries to acquire a lock on A.class as there is only one A.class the lock is obtained and the threads are synchronized. So basically if we have an instance var let say Object o = new Object(); and we synchronize on o it would yield the same result.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread doubt
 
Similar Threads
exam lab gc question
How many Objects are elligible for Gabbage Collection at Line 17?
Doubt over Garbage Collection
Doubt related to Garbage Collection Questions
Q on garbage collection