Java Threads

Download java source code from java.threads.zip.

Java documentation: Thread

Java tutorial Concurrency

Thread Class and Runnable Interface

Thread encapsulates an execution thread and provides methods to manage thread execution. The Runnable interface defines only the method run(). The run method establishes the entry point for another concurrent thread of execution within your program. This thread will end when run() returns.

After you have created a class that implements Runnable, you can instantiate an object of type Thread on an object of that class.

Some simple improvements:

  1. You can have a thread begin executioin as soon as it is created, within the constructor.
  2. You can give a name to a thread, so it is not necssary to create a name variable.

You can extend the Thread class, rather than implement Runnable. You will need to override the run() method. You must also call start() to begin execution of the new thread.

Creating Multiple Threads

Determining when a Thread Ends

Sychronization

Thread Communication

Suspending, Resuming, and Stopping Threads

Reference

Herbert Schildt and Dale Skrien, Java Programming, A Comprehensive Introduction,
McGraw-Hill, 2013. ISBN 978-0-07-802207-4. Chapter 12


Maintained by John Loomis, last updated 20 November 2012