Implementing threads by implementing the runnable interface

22 Apr

Implementing threads by implementing the runnable interface

While we’re not going to go into detail about interfaces, simply put, an Interface in Java is a named collection of method definitions, without implementations. They are instead filled with abstract method declarations. By implementing an interface, you are essentially establishing a contract to implement all of the abstract classes declared in the interface. Our first thread example, the four threaded counting program, can easily be made to implement the Runnable interface.

Listing 3-21: IndependentCounting.java

public class IndependentCounting implements Runnable {
String threadName;
int waitTime;
Thread actualThread;
public IndependentCounting(String tName, int wTime) {
this.threadName = tName;
this.waitTime = wTime;
actualThread = new Thread(this);
actualThread.start();
}
public static void main(String[] args) {
int one = Integer.parseInt(args[0]);
int two = Integer.parseInt(args[1]);
int three = Integer.parseInt(args[2]);
int four = Integer.parseInt(args[3]);
new IndependentCounting(“Thread One Count = “, one);
new IndependentCounting(“Thread Two Count =”, two);
new IndependentCounting(“Thread Three Count = “, three);
new IndependentCounting(“Thread Four Count =”, four);
}
public void run() {
int count = 0;
while(count < 25) {
count++;
System.out.println(this.threadName + “ “ + count);
try {
Thread.sleep((long)waitTime);
} catch(Exception e) {System.out.println(“Couldn’t Sleep”);}
}
}
}

The significant thing to note about this program is that it is implemented inside a single class, whereas our other example had the threads implemented in one class and used in another. Let’s take a closer look. The program begins with a class definition statement, declares three variables, and defines a constructor. Note the use of the keywords implements runnable, which states that it is implementing the Runnable interface. As in the CountingThread example, we have a string variable representing the thread name and an integer representing the user-defined wait time for each thread. We now have an additional variable, actualThread, associated with each IndependentThread object. The constructor initializes the string and the integer as before, but this time it also calls the Thread constructor and calls the start() method of the Thread class. This causes the run() method to be executed.

public class IndependentCounting implements Runnable {
String threadName;
int waitTime;
Thread actualThread;
public IndependentCounting(String tName, int wTime) {
this.threadName = tName;
this.waitTime = wTime;
actualThread = new Thread(this);
actualThread.start();
}

The main() method is executed when the Java program is executed from the command line. It accepts four command line parameters (the integer wait times) and passes them to the IndependentCounting constructor, which is invoked four times. Each time it is invoked, the constructor causes the run() method to be executed.

public static void main(String[] args) {
int one = Integer.parseInt(args[0]);
int two = Integer.parseInt(args[1]);
int three = Integer.parseInt(args[2]);
int four = Integer.parseInt(args[3]);
new IndependentCounting(“Thread One Count = “, one);
new IndependentCounting(“Thread Two Count =”, two);
new IndependentCounting(“Thread Three Count = “, three);
new IndependentCounting(“Thread Four Count =”, four);
}

The run() method is almost identical to the run() method used in the CountingThread example with one exception. When we invoke the sleep() method in this example, we need to use a fully qualified path to ensure the Java compiler understands what class we want the sleep method to be taken from. To do this, we precede it with the class name, Thread.

public void run() {
int count = 0;
while(count < 25) {
count++;
System.out.println(this.threadName + “ “ + count);
try {
Thread.sleep((long)waitTime);
} catch(Exception e) {System.out.println(“Couldn’t Sleep”);}
}
}
}

The program output looks like this:

c:\> javac IndependentCounting.java
c:\> java IndependentCounting 1000 200 500 5000
….
Thread Three Count =                                                                 25
Thread One Count =                                                                   14
Thread One Count =                                                                   15
Thread Four Count =                                                                  4
….

Random Posts

Comments are closed.