Creating and Running Threads in Java

Java was designed with threads in mind, so you'll find it easier to work with threads in Java than in many other languages. To create and start a new thread, just create an instance of the Thread object and call the start() method:

Thread myThread = new Thread();
myThread.start();


Of course, this code won't do anything useful because the thread isn't assigned a task. The JVM creates a new system thread, starts it, and calls the Thread object's run() method. By default, the run() method doesn't do anything, so the thread dies. If you want to give a thread a task, and I'm sure you do, give the run() method something to do. You can do this in three basic ways:

  • Extend the Thread class
  • Implement the Runnable interface
  • Use anonymous inner classes

Extending the Thread Class

A quick way to give a thread a task is simply to extend the Thread class and override the run() method:

public class MyThread extends Thread {
 public void run() {
 System.out.println("Do something cool here.");
 }
}


Then create and start the thread the same way as before:

Thread myThread = new MyThread();
myThread.start();


Now you've got two threads running: the main thread and the thread you just started.

Implementing the Runnable Interface

Extending the Thread class is easy, but most of the time you probably don't want to write a new class just to start a thread. For example, you might want a class that extends another class and can also be run as a thread. In this case, implement the Runnable interface:

public class MyClass extends SomeOtherClass implements Runnable {
 public MyClass() {
 Thread thread = new Thread(this);
 thread.start();
 }
 public void run() {
 System.out.println("Do something cool here.");
 }
}


In this example, the MyClass object starts a new thread on construction. The Thread class takes a Runnable object as a parameter in its constructor, and that Runnable is executed when the thread is started.

Using Anonymous Inner Classes

Sometimes you want to spawn a new thread without the bother of creating a new class, or perhaps it's not convenient to implement the Runnable interface. In this case, you can use an anonymous inner class to start a new thread:

new Thread() {
 public void run() {
 System.out.println("Do something cool here.");
 }
}.start();


This example is simple enough, but it can quickly become unreadable if the code in the run() method is too long. Use this one sparingly.

Waiting for a Thread to Finish

If you want your current thread to wait until a thread is finished, use the join() method:

myThread.join();


This could be useful when a player exits your game, when you want to make sure all threads are finished before you do any cleanup.

Sleepy Threads

Sometimes your threads might get tired, and you'll be nice enough to give them a break. Now you're thinking, "What? My threads get tired? This is too complicated!" No, your threads don't really get tired. But sometimes you might need a thread to pause for a bit, so use the static sleep() method:

Thread.sleep(1000);


This causes the currently running thread to sleep for 1000 milliseconds, or any amount of time you choose. A sleeping thread doesn't consume any CPU timeā€”so it doesn't even dream.



   
Comments