Creating Threads
There are several ways to create a thread in a Java program. Each Java program contains at least one thread: the main path. Other threads are created through the thread constructor or the class that instantiates the thread that inherits the class.
Java threads can create additional threads by directly instantiating the thread object or by instantiating an object that inherits thread. In the example in the thread base (where we compute as many primes as possible within 10 seconds), we create a thread by instantiating the object of the Calculateprimes type (which inherits thread).
When we talk about threads in a Java program, we might mention two related entities: the actual thread that completes the work or thread object that represents it. A running thread is typically created by the operating system, and the thread object is created by the Java VM as a way to control related threads.
Creating and starting threads are not the same
Before a thread invokes the start () method on the thread object of the new threads, the new thread does not really start executing. The thread object already exists before its thread is actually started, and it still exists after its thread exits. This allows you to control or get information about the threads that have been created, even if the thread has not started or has completed.
It is often not a good idea to start the thread through start () in the constructor. Doing so exposes the partially constructed object to the new thread. If the object has a thread, it should provide a start () or init () method that launches the thread instead of starting it from the constructor. (See Resources for links to articles that provide more detailed descriptions of this concept.) )
End Thread
The thread ends in one of the following three ways:
The thread reaches the end of its run () method.
The thread throws an Exception or Error that is not caught.
Another thread invokes a deprecated stop () method. Discarding means that these methods still exist, but you should not use them in new code, and you should try to remove them from existing code as much as possible.
When all the threads in the Java program are complete, the program exits.
Join thread
The thread API contains methods that wait for another thread to complete: the Join () method. When Thread.Join () is invoked, the calling thread blocks until the target thread completes.
Thread.Join () is usually used by programs that use threads to divide large problems into small problems, each with a thread assigned to it. The example at the end of this chapter creates 10 threads, starts them, and then uses Thread.Join () to wait for them all to complete.
Scheduling
In addition to when to use Thread.Join () and object.wait (), the timing of thread scheduling and execution is indeterminate. If two threads are running at the same time and do not wait, you must assume that other threads can run and modify program variables between any two directives. If a thread wants to access a variable that other threads can see, such as data that is referenced directly or indirectly from a static field (global variable), you must use synchronization to ensure data consistency.
In the simple example below, we will create and start two threads, each of which prints two lines to System.out:
public class TwoThreads {
public static class Thread1 extends Thread {
public void run() {
System.out.println("A");
System.out.println("B");
}
}
public static class Thread2 extends Thread {
public void run() {
System.out.println("1");
System.out.println("2");
}
}
public static void main(String[] args) {
new Thread1().start();
new Thread2().start();
}
}