Sub topic 1
What Is a Thread and Why Use More Than One?
Programs, Processes, and Threads
When you run a Java program, your computer starts a process. A process is a running program with its own block of memory. Your web browser is one process. Your music player is another. They do not share memory with each other.
Inside every process there is at least one thread. A thread is a single path of work that the computer follows, one step at a time. It runs your code line by line. A process can hold many threads. All the threads in one process share the same memory, so they can read and change the same objects.
You can think of a process as a kitchen and threads as cooks. The kitchen has one set of shelves and tools. Several cooks can work in it at the same time, and they all use the same shelves.
| Idea | What it is | Shares memory with others? |
|---|---|---|
| Process | A running program | No, each process has its own memory |
| Thread | One path of work inside a process | Yes, with other threads in the same process |
The Main Thread
Every Java program already uses a thread. When you run a class, Java starts a thread called main, and that thread runs your main method. Try this program:
public class WhoAmI {
public static void main(String[] args) {
String name = Thread.currentThread().getName();
int cores = Runtime.getRuntime().availableProcessors();
System.out.println("This code runs on thread: " + name);
System.out.println("This computer has " + cores + " processor cores");
}
}Thread.currentThread() returns the thread that is running the line right now. On most computers the output looks like this:
This code runs on thread: main
This computer has 8 processor coresThe number of cores depends on your machine. A core is a part of the processor that can run one thread at a time. A computer with 8 cores can run 8 threads at exactly the same moment.
Why Use More Than One Thread?
With only one thread, a program does one thing at a time. That is often fine, but sometimes it is too slow.
There are two main reasons to add more threads.
- Doing more work at once. If a job can be split into parts, several threads can work on different parts on different cores. Resizing 1,000 photos goes faster when 4 threads each take 250 photos.
- Not getting stuck while waiting. Some tasks spend most of their time waiting, for example reading a file or downloading a web page. While one thread waits, another thread can do useful work. A server uses this to answer many users at once.
The Cost of Threads
Threads are useful, but each one brings costs.
- Each thread uses memory.
- Switching between threads takes a little time.
- Because threads share memory, two threads can change the same data at once and corrupt it. Later chapters show how this happens and how to stop it.
What Comes Next
You now know what a thread is and why programs use more than one. The next chapter shows how to create and start your own threads with Thread and Runnable.
Exercise
Two threads run inside the same Java program. A third thread runs inside a different program. Which of these threads can read the same objects in memory, and why?
Show answer
Only the two threads in the same program. Threads in one process share that process's memory. A thread in a different program belongs to a different process, and that process has its own separate memory.
Exercise
A program downloads 20 web pages one after another, and most of its time goes to waiting for the network. Name the benefit of using several threads that applies here.
Show answer
Not getting stuck while waiting. While one thread waits for a page to arrive, other threads can start their own downloads, so the total time drops.