Tutoriallearn.com
Easy Learning
Search Tutoriallearn.com :
class T1 extends Thread
{
public void run()
{
try {
for(int n = 5; n > 0; n--) {
System.out.println("Child Thread" + n);
Thread.sleep(1000);
}
}catch (Exception e) {
System.out.println("Child thread interrupted");
}
}
}
class Test
{
public static void main(String args[])
{
try {
T1 a =new T1();
a.start();
for(int n = 5; n > 0; n--) {
System.out.println("main thread"+n);
Thread.sleep(1000);
}
} catch (Exception e) {
System.out.println("Main thread interrupted");
}
}
}
class T1 implements Runnable
{
public void run()
{
try {
for(int n = 5; n > 0; n--) {
System.out.println("Child Thread" + n);
Thread.sleep(1000);
}
}catch (Exception e) {
System.out.println("Child thread interrupted");
}
}
}
class Test1
{
public static void main(String args[])
{
try {
T1 a =new T1();
Thread t = new Thread(a);
t.start();
for(int n = 5; n > 0; n--) {
System.out.println("main thread"+n);
Thread.sleep(1000);
}
} catch (Exception e) {
System.out.println("Main thread interrupted");
}
}
}
public class T1 implements Runnable
{
public void run()
{
try {
for(int n = 5; n > 0; n--) {
//System.out.println("Child Thread" + n);
Thread t = Thread.currentThread();
String name = t.getName();
System.out.println("name=" + name +n);
Thread.sleep(1000);
}
}catch (InterruptedException e) {
System.out.println("Child thread interrupted");
}
}
public static void main(String args[])
{
try {
T1 a1 =new T1();
Thread t1 = new Thread(a1, "First Thread");
t1.start();
T1 a2 =new T1();
Thread t2 = new Thread(a2, "Second Thread");
t2.start();
T1 a3 =new T1();
Thread t3 = new Thread(a3, "Third Thread");
t3.start();
for(int n = 5; n > 0; n--) {
System.out.println("main thread"+n);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted");
}
}
}
name=First Thread5 name=Third Thread5 name=Second Thread5 main thread5 name=First Thread4 name=Second Thread4 main thread4 name=Third Thread4 name=First Thread3 main thread3 name=Second Thread3 name=Third Thread3 name=First Thread2 main thread2 name=Second Thread2 name=Third Thread2 name=First Thread1 name=Second Thread1 main thread1 name=Third Thread1