Thread in Java
Thread in Java
Thread Priorities
Every Java thread has a priority that helps the operating system determine the
order in which threads are scheduled.
Java thread priorities are in the range between MIN_PRIORITY (a constant of 1)
and MAX_PRIORITY (a constant of 10). By default, every thread is given priority
NORM_PRIORITY (a constant of 5).
Threads with higher priority are more important to a program and should be
allocated processor time before lower-priority threads. However, thread priorities
cannot guarantee the order in which threads execute and are very much platform
dependent.
Step 1
Step 2
Step 3
Example
Output
Creating Thread-1
Starting Thread-1
Creating Thread-2
Starting Thread-2
Running Thread-1
Thread: Thread-1, 4
Running Thread-2
Thread: Thread-2, 4
Thread: Thread-1, 3
Thread: Thread-2, 3
Thread: Thread-1, 2
Thread: Thread-2, 2
Thread: Thread-1, 1
Thread: Thread-2, 1
Thread Thread-1 exiting.
Thread Thread-2 exiting.
Step 1
You will need to override run( ) method available in Thread class. This method
provides an entry point for the thread and you will put your complete business logic
inside this method. Following is a simple syntax of run() method −
public void run( )
Step 2
Example
Output
Creating Thread-1
Starting Thread-1
Creating Thread-2
Starting Thread-2
Running Thread-1
Thread: Thread-1, 4
Running Thread-2
Thread: Thread-2, 4
Thread: Thread-1, 3
Thread: Thread-2, 3
Thread: Thread-1, 2
Thread: Thread-2, 2
Thread: Thread-1, 1
Thread: Thread-2, 1
Thread Thread-1 exiting.
Thread Thread-2 exiting.
Thread Methods
Following is the list of important methods available in the Thread class.
1
public void start()
Starts the thread in a separate path of execution, then invokes the run() method on this Thread
object.
2
public void run()
If this Thread object was instantiated using a separate Runnable target, the run() method is
invoked on that Runnable object.
3
public final void setName(String name)
Changes the name of the Thread object. There is also a getName() method for retrieving the
name.
4
public final void setPriority(int priority)
Sets the priority of this Thread object. The possible values are between 1 and 10.
5
public final void setDaemon(boolean on)
A parameter of true denotes this Thread as a daemon thread.
6
public final void join(long millisec)
The current thread invokes this method on a second thread, causing the current thread to block
until the second thread terminates or the specified number of milliseconds passes.
7
public void interrupt()
Interrupts this thread, causing it to continue execution if it was blocked for any reason.
8
public final boolean isAlive()
Returns true if the thread is alive, which is any time after the thread has been started but before it
runs to completion.
The previous methods are invoked on a particular Thread object. The following
methods in the Thread class are static. Invoking one of the static methods performs
the operation on the currently running thread.
1
public static void yield()
Causes the currently running thread to yield to any other threads of the same priority that are
waiting to be scheduled.
2
public static void sleep(long millisec)
Causes the currently running thread to block for at least the specified number of milliseconds.
3
public static boolean holdsLock(Object x)
Returns true if the current thread holds the lock on the given Object.
4
public static Thread currentThread()
Returns a reference to the currently running thread, which is the thread that invokes this method.
5
public static void dumpStack()
Prints the stack trace for the currently running thread, which is useful when debugging a
multithreaded application.
Example
Following is the main program, which makes use of the above-defined classes −
// File Name : ThreadClassDemo.java
public class ThreadClassDemo {
System.out.println("Starting thread3...");
Thread thread3 = new GuessANumber(27);
thread3.start();
try {
thread3.join();
} catch (InterruptedException e) {
System.out.println("Thread interrupted.");
}
System.out.println("Starting thread4...");
Thread thread4 = new GuessANumber(75);
thread4.start();
System.out.println("main() is ending...");
}
}
This will produce the following result. You can try this example again and again and
you will get a different result every time.
Output
Starting hello thread...
Starting goodbye thread...
Hello
Hello
Hello
Hello
Hello
Hello
Goodbye
Goodbye
Goodbye
Goodbye
Goodbye
.......
o Thread()
o Thread(String name)
o Thread(Runnable r)
o Thread(Runnable r,String name)
If you are not extending the Thread class,your class object would not be treated as a th
create Thread class object.We are passing the object of your class that implements Run
may execute.