Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I am getting diff output when i use Synchronized?

 
sabapathy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in this case,does keyword "synchronized " make any sense?
running it with and without synchronized seems produce same results
 
Sagar Sharma
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic