Hi all,
I ran this program on jdk1.5. Anybody Please explain output and why...
public class
Thread extends Thread{
public synchronized void method1() throws Exception {
System.out.println(Thread.currentThread().getName() + " method1");
Thread.sleep(10*1000);
System.out.println(Thread.currentThread().getName() + " method1 done");
}
public synchronized void method2() throws Exception {
System.out.println(Thread.currentThread().getName() + " method2");
Thread.sleep(10*1000);
System.out.println(Thread.currentThread().getName() + " method2 done");
}
public void run() {
try {
method1();
//System.out.println("method 1 Done");
method2();
System.out.println(Thread.currentThread().getName()+" Done");
}catch(Exception e) {
e.printStackTrace();
}
}
public static void main(
String args[]) {
Thread1 t1 = new Thread1();
Thread1 t2 = new Thread1();
t1.setName("T1");
t2.setName("T2");
t1.setPriority(8);
t2.setPriority(9);
t1.start();
t2.start();
System.out.println("Done");
}
}
Output:
T1 method1
T2 method1
Done
T2 method1 done
T2 method2
T1 method1 done
T1 method2
T2 method2 done
T2 Done
T1 method2 done
T1 Done