The Artima Developer Community
Sponsored Link

Java Answers Forum
Thread Synchronization

1 reply on 1 page. Most recent reply: Jun 18, 2009 11:25 AM by Matthew Flower

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
manoj nanda

Posts: 1
Nickname: manojkumar
Registered: May, 2009

Thread Synchronization Posted: May 6, 2009 2:12 AM
Reply to this message Reply
Advertisement
Dear Experts,
In my following code I have createed a synchronized method called print() within class Printer. Two threads are accessing the same printer instance and doing their printing job. Even if print() is synchronized, it allows both the threads to enter inside. But if I change my code with synchronized block by getting a lock Printer.class, one thread enters at a time. Why it is so?

I have an understanding that with a synchronized print() method I should be able to controll entering of one thread at a time, which seems to be not correct.

public class TestSync {
Printer printer = new Printer();
public static void main(String[] args) {
ThreadA a = new TestSync().new ThreadA();
//a.setName("Manoj");
ThreadB b = new TestSync().new ThreadB();
//b.setName("Gopi");
b.start();
a.start();
}

class ThreadA extends Thread {
public void run() {
//synchronized (Printer.class) {
printer.print();
//}
}
}
class ThreadB extends Thread {
public void run() {
//synchronized (Printer.class) {
printer.print();
//}
}
}
}
class Printer {
public synchronized void print() {
for (int i = 0; i < 10; i++) {
System.out.println("I am in " + Thread.currentThread().getName());
try{
Thread.sleep(100);
}catch(InterruptedException e){}
}
}
}

output
--------
I am in Thread-0
I am in Thread-1
I am in Thread-0
I am in Thread-1
I am in Thread-0
I am in Thread-1
I am in Thread-0
I am in Thread-1
I am in Thread-0
I am in Thread-1
I am in Thread-0
I am in Thread-1
I am in Thread-0
I am in Thread-1
I am in Thread-0
I am in Thread-1
I am in Thread-0
I am in Thread-1
I am in Thread-0

Thanks and Regards,
Manoj


Matthew Flower

Posts: 1
Nickname: mattflower
Registered: Jan, 2008

Re: Thread Synchronization Posted: Jun 18, 2009 11:25 AM
Reply to this message Reply
Your trouble has to do with what you are synchronizing on.

When you add the synchronized keyword on an instance method, you are synchronizing on the instance of that class.

Earlier, when you called:

ThreadA a = new TestSync().new ThreadA();
ThreadB b = new TestSync().new ThreadB();

There were two instances of TestSync and therefore two instances of the printer class. Your threads seemed to be independent because they were using two different classes for locking.

When you instead synchronize on Printer.class, you are synchronizing on a single instance. As odd as it may sound, Printer.class is a class itself which has nothing to do with your individual instances.

Hope that helps,
Matt

Flat View: This topic has 1 reply on 1 page
Topic: problem using fast code eclipse plugin Previous Topic   Next Topic Topic: How to get resultant page in different frame using JSF component

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use