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.
------------------