Threads
Threads
Java - Multithreading
Advertisements
Multi-threading enables you to write in a way where multiple activities can proceed
concurrently in the same program.
https://www.tutorialspoint.com/java/java_multithreading.htm 1/9
27/03/2018 Java Multithreading
New − A new thread begins its life cycle in the new state. It remains in this state
until the program starts the thread. It is also referred to as a born thread.
Runnable − After a newly born thread is started, the thread becomes runnable. A
thread in this state is considered to be executing its task.
Waiting − Sometimes, a thread transitions to the waiting state while the thread waits
for another thread to perform a task. A thread transitions back to the runnable state
only when another thread signals the waiting thread to continue executing.
Timed Waiting − A runnable thread can enter the timed waiting state for a specified
interval of time. A thread in this state transitions back to the runnable state when that
time interval expires or when the event it is waiting for occurs.
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
As a first step, you need to implement a run() method provided by a Runnable interface.
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 the run() method −
Step 2
As a second step, you will instantiate a Thread object using the following constructor −
Where, threadObj is an instance of a class that implements the Runnable interface and
threadName is the name given to the new thread.
Step 3
Once a Thread object is created, you can start it by calling start() method, which executes a
call to run( ) method. Following is a simple syntax of start() method −
https://www.tutorialspoint.com/java/java_multithreading.htm 2/9
27/03/2018 Java Multithreading
void start();
Example
Here is an example that creates a new thread and starts running it −
Live Demo
class RunnableDemo implements Runnable {
private Thread t;
private String threadName;
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
https://www.tutorialspoint.com/java/java_multithreading.htm 3/9
27/03/2018 Java Multithreading
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 −
Step 2
Once Thread object is created, you can start it by calling start() method, which executes a
call to run( ) method. Following is a simple syntax of start() method −
void start( );
Example
Here is the preceding program rewritten to extend the Thread −
Live Demo
class ThreadDemo extends Thread {
private Thread t;
private String threadName;
https://www.tutorialspoint.com/java/java_multithreading.htm 4/9
27/03/2018 Java Multithreading
}
}
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 Starts the thread in a separate path of execution, then invokes the run() method
on this Thread object.
2 If this Thread object was instantiated using a separate Runnable target, the run()
method is invoked on that Runnable object.
3 Changes the name of the Thread object. There is also a getName() method for
retrieving the name.
https://www.tutorialspoint.com/java/java_multithreading.htm 5/9
27/03/2018 Java Multithreading
Sets the priority of this Thread object. The possible values are between 1 and 10.
The current thread invokes this method on a second thread, causing the current
6
thread to block until the second thread terminates or the specified number of
milliseconds passes.
7 Interrupts this thread, causing it to continue execution if it was blocked for any
reason.
8 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 Causes the currently running thread to yield to any other threads of the same
priority that are waiting to be scheduled.
2 Causes the currently running thread to block for at least the specified number of
milliseconds.
4 Returns a reference to the currently running thread, which is the thread that
invokes this method.
https://www.tutorialspoint.com/java/java_multithreading.htm 6/9
27/03/2018 Java Multithreading
Prints the stack trace for the currently running thread, which is useful when
debugging a multithreaded application.
Example
The following ThreadClassDemo program demonstrates some of these methods of the Thread
class. Consider a class DisplayMessage which implements Runnable −
Following is the main program, which makes use of the above-defined classes −
https://www.tutorialspoint.com/java/java_multithreading.htm 7/9
27/03/2018 Java Multithreading
thread2.setDaemon(true);
System.out.println("Starting goodbye thread...");
thread2.start();
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
.......
Advertisements
https://www.tutorialspoint.com/java/java_multithreading.htm 8/9
27/03/2018 Java Multithreading
YouTube 69K
https://www.tutorialspoint.com/java/java_multithreading.htm 9/9