javascript
Brief description  about Online courses   join in Online courses
View NELSON  THOMAS 's Profile

Threads using class Thread

class ThreadEx extends Thread {

ThreadEx() {
}
ThreadEx(String threadName) {
super(threadName); // Initialize thread.
System.out.println(this);
start();
}
public void run() {
//Display info about this particular thread
System.out.println(Thread.currentThread().getName());
}
}

public class ThreadExample {

public static void main(String[] args) {
Thread thread1 = new Thread(new ThreadEx(), "thread1");
Thread thread2 = new Thread(new ThreadEx(), "thread2");
// The below 2 threads are assigned default names
Thread thread3 = new ThreadEx();
Thread thread4 = new ThreadEx();
Thread thread5 = new ThreadEx("thread5");
//Start the threads
thread1.start();
thread2.start();
thread3.start();
thread4.start();
try {
//The sleep() method is invoked on the main thread to cause a one second delay.
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
}
//Display info about the main thread
System.out.println(Thread.currentThread());
}
}

OUTPUT:
Thread[thread5,5,main]
thread1
thread5
thread2
Thread-3
Thread-2
Thread[main,5,main]

I have a doubt in this programme why the thread4 is not executed and in the result given how the bar('-") came in result Thread-3and Thread-2
Asked by NELSON THOMAS | Feb 20, 2011 |  Reply now
Replies (1)
View arun kumar das 's Profile
HI

Please check your code.

java teacher
Feb 22, 2011