Creating threads with java.lang.Thread class
The following program (CreateThread.java) demonstrates a method for creating threads with the java.lang.Thread class. The program code is given below for analysis. At the bottom, the fields, constructors and methods of the Thread class are also described for ready reference.
CreateThread.java
class MyThread extends Thread {
MyThread(String name) {
super(name);
System.out.println(name + " started.");
start();
}
@Override
public void run() {
try {
for(int i=0;i<5;i++){
System.out.println(Thread.currentThread().getName() + " is running..");
Thread.sleep(500);
}
}
catch(InterruptedException e) {
System.out.println(e);
}
System.out.println(Thread.currentThread().getName() + " ended.");
}
}
public class CreateThread {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName() + " started.");
// Creating object of MyThread class
MyThread t = new MyThread("MyThread");
try {
for (int i = 0; i < 5; i++) {
System.out.println((Thread.currentThread()).getName()
+ " thread is running...");
Thread.sleep(500);
}
}
catch (InterruptedException e) {
System.out.println(e);
}
System.out.println(Thread.currentThread().getName() + " ended.");
}
}
Output:
First run of the program is given below.
main started.
MyThread started.
main thread is running...
MyThread is running..
main thread is running...
MyThread is running..
main thread is running...
MyThread is running..
main thread is running...
MyThread is running..
main thread is running...
MyThread is running..
main ended.
MyThread ended.
Second run of the program is given below.
main started.
MyThread started.
main thread is running...
MyThread is running..
MyThread is running..
main thread is running...
MyThread is running..
main thread is running...
MyThread is running..
main thread is running...
MyThread is running..
main thread is running...
MyThread ended.
main ended.
Explanation
It is to be noted that each run of the program produces different output sequences in terms of thread execution. In fact, as programmers, we can’t control the execution sequences of threads. Basically, the execution sequences of threads is controlled by the thread scheduler. That is why the output is completely unpredictable.
A closer look at the java.lang.Thread Class
Thread class signature:
public class Thread extends Object implements Runnable
Thread class is created from Object class and Runnable Interface.
Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.
When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:
- The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
- All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.
Field Description
Field Summary | |
static int | MAX_PRIORITY |
static int | MIN_PRIORITY |
static int | NORM_PRIORITY |
Thread has following Range of Priority
/**
* The minimum priority that a thread can have.
*/
public final static int MIN_PRIORITY = 1;
/**
* The default priority that is assigned to a thread.
*/
public final static int NORM_PRIORITY = 5;
/**
* The maximum priority that a thread can have.
*/
public final static int MAX_PRIORITY = 10;
Constructor Description
Constructor Summary |
Thread() |
Thread(Runnable target) |
Thread(Runnable target, String name) |
Thread(String name) |
Thread(ThreadGroup group, Runnable target) |
Thread(ThreadGroup group, Runnable target, String name) |
Thread(ThreadGroup group, Runnable target, String name, long stackSize) |
Thread(ThreadGroup group, String name) |
Method Description
Method Summary | |
static int | activeCount() |
void | checkAccess() |
int | countStackFrames() |
static Thread | currentThread() |
void | destroy() |
static void | dumpStack() |
static int | enumerate(Thread[] tarray) |
ClassLoader | getContextClassLoader() |
String | getName() |
int | getPriority() |
ThreadGroup | getThreadGroup() |
static boolean | holdsLock(Object obj) |
void | interrupt() |
static boolean | interrupted() |
boolean | isAlive() |
boolean | isDaemon() |
boolean | isInterrupted() |
void | join() |
void | join(long millis) |
void | join(long millis, int nanos) |
void | resume() |
void | run() |
void | setContextClassLoader(ClassLoader cl) |
void | setDaemon(boolean on) |
void | setName(String name) |
void | setPriority(int newPriority) |
static void | sleep(long millis) |
static void | sleep(long millis, int nanos) |
void | start() |
void | stop() |
void | stop(Throwable obj) |
void | suspend() |
String | toString() |
static void | yield() |
In the next section, the technique for creating threads with the Runnable interface is given.