Java Multi Threading
Java Multi Threading
An Introduction to
Multi-threading
12 MARCH 2008 | LINUX For You | www.openITis.com
cmyk
Overview
I
n a typical application like a word created in main).
processor, many tasks need to be executed
in parallel—for instance, responding to Asynchronous execution
the user, checking spellings, formatting, Note that created threads run ‘asynchronously’. This
displaying, saving, etc. These tasks can means that threads do not run sequentially (like function
run as different processes, sharing the resources calls); rather, the order of execution of threads is not
using inter-process communication. Multi-processing predictable. To understand this, let us extend our simple
is heavyweight and costly in terms of the system calls program here:
made, and requires a process switch.
An easier and cheaper alternative is to use threads. class MyThread extends Thread {
Multiple threads can run in the context of the same public void loop() {
process and hence share the same resources. A context for(int i = 0; i < 3; i++) {
switch is enough to switch between threads, whereas a System.out.println(“In thread “ +
costly process switch is needed for processes. Thread.currentThread().getName() +
Java has support for concurrent programming and “; iteration “ + i);
has support for multi-threading in both language and try {
library levels. In this article, we’ll cover the basics of Thread.sleep((int)Math.random());
multi-threaded programming in Java and write some }
simple programs. We’ll also look at some difficult catch(InterruptedException ie) {
problems that can crop up when we write multi- ie.printStackTrace();
threaded code. }
}
The basics }
A Java thread can be created in two ways: by
implementing the Runnable interface or by extending public void run() {
the Thread class. Both have a method called run. This System.out.println(“This is new thread”);
method will be called by the runtime when a thread this.loop();
starts executing. }
A thread can be created by invoking the start method
on a Thread or its derived objects. This in turn calls the public static void main(String args[]) throws Exception {
run method and keeps the thread in running state. Now MyThread mt = new MyThread();
let us look at a simple example using threads. mt.start();
System.out.println(“This is the main Thread”);
class MyThread extends Thread { mt.loop();
public void run() { }
System.out.println(“This is new thread”); }
}
In a sample run, the output was:
public static void main(String args[]) throws Exception {
MyThread mt = new MyThread(); This is the main Thread
mt.start(); In thread main; iteration 0
System.out.println(“This is the main Thread”); This is new thread
} In thread Thread-0; iteration 0
} In thread Thread-0; iteration 1
In thread main; iteration 1
This program prints: In thread Thread-0; iteration 2
In thread main; iteration 2
This is the main Thread
This is new thread In this program, we have a loop method that has a for
loop that has three iterations. In the for loop, we print
In this example, the MyThread class extends the the name of the thread and the iteration number. We can
Thread class. We need to override the run method set and get the name of the thread using setName and
in this class. This run method will be called when the getName methods, respectively. If we haven’t set the
thread runs. In the main function, we create a new name of the thread explicitly, the JVM gives a default
thread and start it. The program prints messages name (‘main’, ‘Thread-0’, ‘Thread-1’, etc.). After printing
from both the main thread and the mt thread (that we this information, we force the current thread to sleep for
cmyk
Overview
cmyk
Overview
a ‘deadlock’ (there are other problems like ‘livelocks’ Thread t2 = new Thread(c);
cmyk