Many programs, particularly those with a graphical user interface, are multi-threaded.
Suppose a spreadsheet program allows a single user:
or
suppose a video game:
Multithreading can be more efficient than having to run separate programs.
Consider a database server to allows hundreds or thousands of simultaneous users. The application that supports these users might create a thread for each user. Suppose a married couple share a bank account and both happen to be using two different ATMs to withdraw money at the same time. In this case, having a "lock" on the account might be useful, so that they do not withdraw twice the money in the account.
The JVM supports multitasking through multithreaded programming, with each thread belonging to the same process (or application). In many cases, multithreading can improve performance. However, if there are too many threads, the overhead of thread management (context switching) can degrade performance.
Although we never had to think about it before, even HelloWorld made use a thread, namely, the main thread.
The Thread class implements the Runnable interface and provides methods such as:
Because Thread is a class, you can extend it.
Your program can run multiple threads concurrently.
One scenario is that you want the main thread to finish last. Instead of calling isAlive() multiple times, you can use join(). In this case, the main thread is calling its children threads to join it, so the main thread will finish last.
You can set the priority of a thread with the setPriority method (see lines 44-45).
The JVM has a monitor that locks an object so that only one thread has access to it. This is useful for preserving data integrity. Line 6 declares the sumArray method to be synchronized, which means that the sum field can be updated by only one thread at a time.
The output is:
Child #1 starting.
Running total for Child #1 is 1
Child #2 starting.
Running total for Child #1 is 3
Running total for Child #1 is 6
Running total for Child #1 is 10
Running total for Child #1 is 15
Sum for Child #1 is 15
Child #1 terminating.
Running total for Child #2 is 1
Running total for Child #2 is 3
Running total for Child #2 is 6
Running total for Child #2 is 10
Running total for Child #2 is 15
Sum for Child #2 is 15
Child #2 terminating.
However, if Line 6 lacks the keyword synchronized, the output is reflects the mixing of threads:
Child #1 starting.
Running total for Child #1 is 1
Child #2 starting.
Running total for Child #2 is 1
Running total for Child #1 is 3
Running total for Child #2 is 5
Running total for Child #1 is 8
Running total for Child #2 is 11
Running total for Child #1 is 15
Running total for Child #2 is 19
Running total for Child #2 is 24
Running total for Child #1 is 29
Sum for Child #1 is 29
Child #1 terminating.
Sum for Child #2 is 29
Child #2 terminating.
This examples was about synchronizing access to a method. Another way to synchronize is by using a synchronized block.
The notify method (line 9 ) allows one thread to communicate to waiting threads that an object is now unlocked and available. The wait method (line 20) forces a thread to stand by until it is notified that the object has become accessible. These two methods are derived from java.lang.Object. The concept of working with threads is therefore deeply fundamental to the Java programming language.
A thread can be running (executing), suspended (idle), or stopped (terminated).