Definition
Threads and processes are the two most important components of concurrent programming.The term thread describes a task running inside a process. Each process has at least one thread. The process of duplication of threads inside a process is called fork. The implementation of threads and processes depends on the operating system (OS).
Introduction to Java Threads
Java do not support multiple inheritance so there are two ways to create an instance of thread. Both of the approaches should implement the abstract method run and method start should be used to execute the thread.
To implement interface Runnable
Method run should be implemented and should be used to start the thread.
Example
Class ThreadEx1 implements the interface Runnable and creates an eternal loop which prints a new line with the thread's id.
public class ThreadEx1 implements Runnable
{
private String m_sMsg;
public ThreadEx1(int nThreadId)
{
m_sMsg = "Thread #";
m_sMsg += Integer.toString(nThreadId);
}
public void run() {
while (true)
{
System.out.println(m_sMsg);
}
}
}
A console application which creates and executes two thread.
/**
*
* @author Leon Anavi
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Thread Trd1 = new Thread( new ThreadEx1(1) );
Thread Trd2 = new Thread( new ThreadEx1(2) );
Trd1.start();
Trd2.start();
}
}
Output
Please note that the threads are not synchronized and because of this the output will look strange.
Thread #1
Thread #1
Thread #1
Thread #2
Thread #2
Thread #2
Thread #2
To extend class Thread
Class Thread implement the interface Runnable and introduces methods for managing the thread such as start, join, destroy, etc. Please note that methods stop, resume and suspend are deprecated and should not be used.
Example
public class ThreadEx2 extends Thread
{
private String m_sMsg;
public ThreadEx2(int nThreadId)
{
m_sMsg = "Thread #";
m_sMsg += Integer.toString(nThreadId);
}
@Override public void run() {
while (true)
{
System.out.println(m_sMsg);
}
}
}
/**
*
* @author Leon Anavi
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ThreadEx2 Trd1 = new ThreadEx2(1);
ThreadEx2 Trd2 = new ThreadEx2(2);
Trd1.start();
Trd2.start();
}
}
Output
The output will be the same as the output of the previos example.
Thread #1
Thread #1
Thread #1
Thread #2
Thread #2
Stopping a Thread
Method stop is unsafe and should not be used. Instead method join should be called to stop a threat. Method join waits the threads to die. As an argument of the method a timeout can be set.
Example
The example demonstrates a thread that ends on a condition (number of loops) and the program will ends after termination of instance of the thread.
public class ThreadEx3 extends Thread {
private int m_nStop;
public ThreadEx3(int nStop)
{
m_nStop = nStop;
}
@Override public void run() {
int nIter = 0;
while (nIter < m_nStop)
{
System.out.println("Hello world.");
nIter++;
}
}
}
/**
*
* @author Leon Anavi
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ThreadEx3 Trd1 = new ThreadEx3(3);
Trd1.start();
try {
Trd1.join();
} catch (InterruptedException ex) {
// ...
}
}
Output
The console application will display message Hello world. three times and will exit.
run:
Hello world.
Hello world.
Hello world.
BUILD SUCCESSFUL (total time: 0 seconds)
Further Reading
Lesson: Concurrency
Class Reference
Class Thread
Interface Runnable
|