Create three threads and the main thread. Execute each thread as simultaneous tasks. Display information when exiting each thread.
public class sync extends Thread {
public void run() {
synchronized (this) {
for (int i = 5; i > 0; i--) {
System.out.print("Thread Name :" + Thread.currentThread().getName() + i+"\n");
}
}
}
}
class demo {
public static void main(String args[]) {
sync obj1 =new sync();
sync obj2 =new sync();
sync obj3 =new sync();
obj1.setName("First");
obj2.setName("Second");
obj3.setName("Third");
obj1.start();
obj2.start();
obj3.start();
}
}
O/p:
Thread Name :First5 Thread Name :First4 Thread Name :First3 Thread Name :First2 Thread Name :First1 Thread Name :Second5 Thread Name :Second4 Thread Name :Second3 Thread Name :Second2 Thread Name :Second1 Thread Name :Third5 Thread Name :Third4 Thread Name :Third3 Thread Name :Third2 Thread Name :Third1
HOPE THIS HELPS :)