Looking inside the Thread class

Thread class has several constructors.

public Thread() – Constructs a new thread object whose name is “Thread” concatenated with a digit like “Thread1, Thread2 etc.

public Thread(Runnable target) — Constructs a new Thread object. Target is a object of type Runnable whose run() method is called.

public Thread(String threadname) – Constructs new thread object with name threadname.

The code that does the real wok of a thread is placed in its run() method. The run() method can be overridden is a sub class of Thread.

A program launches a threads execution by calling the thread’s start method which, in turn, calls the run method. When start launches the thread, start returns to its caller immediately. The caller then executes concurrently with the launched thread. The start method throws an IllegalThreadStateException if the thread is trying to start has already been started.

The static sleep method is called with no arguments specifying how long the currently executing thread should sleep. While thread sleeps, it does not contend for the processor, so other threads can executes. This can give lower priority threads a chance to run.

The interrupt method is called to interrupt a thread , The interrupt method returns true if a thread has been interrupted and false otherwise.

Method suspend suspends a threads execution. Method resume resumes the execution of a suspended thread.
Method stop stops a thread by throwing a ThreadDeath object. Method isAlive return true if start has been called for given thread and the thread is not dead.

Method setName set the name of the thread, and getName() returns the name of the thread.

A thread priority can be adjusted with the setPriority which takes an integer argument in the range 1 to 10. If the argument is not in the range 1 through 10 inclusive, then method setPriority throws IllegalArgumentException. Every Java thread priority ranges between Thread.MIN_PRIORITY ( a constant of 1) and Thread.MAX_PRIORITY( a constant of 10) Thread.NORM_PRIORITY (a constant of 5) method getPriority returns thread priorities.

It's very calm over here, why not leave a comment?

Leave a Reply