This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes I am getting diff output when i use Synchronized? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "I am getting diff output when i use Synchronized?" Watch "I am getting diff output when i use Synchronized?" New topic
Author

I am getting diff output when i use Synchronized?

sabapathy
Greenhorn

Joined: Oct 16, 2000
Posts: 10
Review the following code:
class T10 extends Thread
{
public static void main(String args[])
{
T10 thread1 = new T10("Thread1:");
T10 thread2 = new T10("Thread2:");
thread1.start();
thread2.start();
}
public T10(String id)
{
super(id);
}
public void run()
{

displayList(getName());
}
public synchronized void displayList(String name)
{
for(int i=0;i<20;++i)
{
System.out.println(name+i);
try
{
T10 t = (T10)Thread.currentThread();
t.sleep(1000);
}catch(InterruptedException e){}
}
}
}
NOTE: In the displayList method if i use public static keyword then i am getting output as thread1 starting from 1 to 19 after that thread2 starting 1 to 19.But if i use only public synchronized i am not getting the result like above.Why?
Can some one explain me why? I am preparing for SCJP2.
Thanks for your answer.
Sakthivel.
------------------
poorna k
Greenhorn

Joined: Oct 16, 2000
Posts: 7
if you use public static keyword for displayList() method,you are acquiring a class level lock.So the first thread to acquire the lock on the class retains the lock until it finishes execution.note that there is no wait() call within the synchronized method.so the thread makes no attempt to give up the lock even tho' it sleeps.
in the other case,when public keyword is used before displayList() method,the 2 threads are started on different objects,and are free to execute the synchronized method on the respective object that they have acquired a lock on.
am i right?
sabapathy
Greenhorn

Joined: Oct 16, 2000
Posts: 10
Hi Poorna,
As soon as the word synchronized used in the code that means lock has been acquired already on that object.Am i right?
Correct me if i am wrong.
Thread1 starts first then it will acquire that display method.
Once thread1 finishes his execution then only it should execute thread2. I need to find out why it is not doing?.But if you use static keyword it is doing correctly the way i expected.
What is the purpose of using synchronized word in the code.
Thanks for your reply.
Sakthivel.
Bin Zhao
Ranch Hand

Joined: Oct 04, 2000
Posts: 73
in this case,does keyword "synchronized " make any sense?
running it with and without synchronized seems produce same results
Sagar Sharma
Ranch Hand

Joined: Aug 31, 2000
Posts: 92
hi,
using a static keyword for the method makes the lock class-level and then there exists only one copy of the method for all instances. Now if a thread starts the method and does not give up the lock by wait() then all other methods have to wait before this lock is given up.
synchronized keyword ensures that at a time 1 thread is running on the method and nothing else and hence even without it the code runs similar because we have coded in a way that threads get to work alternatively.
Sagar
rob mcfarland
Greenhorn

Joined: Oct 04, 2000
Posts: 11
Sagar,
My understanding is that even if there is a static synchronized method which has obtained a class level lock, non-static synchronized methods can still be run, as they'll be trying to obtain a lock on the object itself so there is no contention. (I've written some code to prove this) Non-synchronized method can be run willy-nilly irrespective of what locks static or non-static synchronized methods have as they're not bothered about locks at all.
Cheers,
Rob.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I am getting diff output when i use Synchronized?
 
Similar Threads
join() method usage
problem with monitors :
Threading issue
locking??
Need Explanation of unpredicted output even if methods are synchronized