Understanding Java Threads

A thread lets you run two or more parts of a program seemingly simultaneously. The Thread class makes an object that can be run as a thread.

The Thread class methods needed are..

Thread(String name) – makes a basic thread and names it – name optional

Thread(Runnable target, Stringname) – turns any object that uses the interface “Runnable” into a thread. Name optional.

static int activeCount() – returns the number of active threads.

static int enumerate(Thread[] t) – fills an array with copy of active thread.

String getName() – Gets the name of the thread.

int getPriority – shows the priority of the thread.

void interrupt() – interrupts the thread.

boolean isInterrupted() – checks if the thread has been interrupted.

void setPriority(int priority) – sets the priority of the thread.

void setName(String name) – names the thread

static void Sleep(int milliseconds) – puts the thread to sleep for x amount of time.

void run() – this method is what is run immediately when the thread is.

void start() – starts the thread.

static void yield() – halts the thread so others can run

Now to get a thread working write a class extending the Thread class. This will make a copy of it that you can customize. This should be in a class outside your main programs class. It should look like…
public class MyThread1 extends Thread{

public void run()
System.out.println(“This thread is running”);
System.out.println(“It will execute it’s commands…”);
System.out.println(“And the program will end…”);

Now create another thread class and make it look like…
public class MyThread2 extends Thread{
int x = 0;
public void run()
while (x < 1000){
x = x + 1;


and make your ThreadExample.java class (the main class of the program) should look like…

public class ThreadingExample {

public static void main(String[] args) {
MyThread1 my_thread_object1 = new MyThread1();
MyThread2 my_thread_object2 = new MyThread2();



That’s the basics. We run the counting thread (the second thread) first and it continues to run counting up to 1000, but the thread that just displays a message kicks in and somewhere in the middle of the counting the message will appear. Where exactly depends on priorities, the exact timing of the computer, etc. My message appeared after the number 510 appeared and then 511 appeared and the counting finished.

Now the limit to using extends is that a class can only extend one superclass so if you write a class to extend Thread your thread can’t extend any other class, which is a serious limitation. In cases like these you want to use the Runnable interface instead. All we need to do is write a class that implements Runnable, provide a run method (which is required as implemented classes always have something which is required), create an instance of the class and pass the Runnable object to its constructor as a parameter, and the call the classes start method. It sounds complex but it’s not.

In our main method we would have a line like…

RunnableClass my_runnable_class = new RunnableClass;

Thread t = new Thread(my_runnable_class);


Or we could use a single line to declare the object as we pass it into the constructor of the Thread class instance and run the start method for the RunnableClass immediately like…

new Thread(new RunnableClass()).start();

Pretty simple. We just need to write our RunnableClass. Remember because we are using an implements keyword we MUST supply the required methods to the class. The required method in this case is called run. Let’s look at the main class though first that will be the heart of the program…
public class ThreadingExample2 {

public static void main(String[] args) {
new Thread(new RunnableClass()).start();

Simple. It just starts a new Thread (we don’t have to write a Thread class here, it’s part of of the java standard classes. To start it we call the Thread class and use it’s construct method to make an instance of it. Basic stuff so far. But when we do it we pass our custom class called RunnableClass into it and immediately run the start method of the RunnableClass. We won’t have to write the start method because it’s standard when we implement Runnable. Unless we needed to override it for some reason we don’t even have to worry about it. It’s already there. So now we write our RunnableClass and it looks like…

public class RunnableClass implements Runnable {

public void run() {
System.out.println(“This thread is running.”);

The import thing there is the word “implements” which means we are making a copy of the Runnable class for our use, but in doing so we leave the option to put the word extends and another class after the word Runnable, which gives us more flexibility later on. But the implements technique forces use to write a run method overriding the basic run method. The part that says “@Override” is just a comment put in automatically by my IDE and is not required, but it gives you an idea of what we are doing here. We are writing a custom version of the run method overriding the standard one. This run method will automatically run as soon as the thread is started. Now this program doesn’t really do much but we could make it behave like our last one if we wanted to first starting a counting sequence and then starting another thread that does something during that count. But basically you get the idea. This thread will run while the program is doing other things and will display a message just as before, but in this case we have the option of still extending another class if we want to because we used an “implements”, instead of “extends”.