7.2.1 What are Threads?

In essence a computer program is a series of instructions performed by a computer. Quite often it makes sense to do several actions at the same time, in parallel. In a thread you can put the sequence of instructions that belongs to one of the parallel actions.

Threads are parallel lines of execution in an application.

A multi-processor computer may actually do these actions simultaneously, a single processor computer will divide its attention chronologically over the threads. In any case, the java code is the simple: it tells only which threads are running. The JVM takes care of the time sharing among the threads.

In the following applet, we visualize threads. The Ball class is a subclass of the class Thread, so each ball represents a thread. Clicking on the ball, will stop the thread and will make the ball disappear.

Examples

Typical examples of programs that use threads are: