java.lang.Thread
As everything in Java (well, almost) is object, if we want to start a new thread, we will need a class that represents the thread. This class is java.lang.Thread
built into the JDK. When you start a Java code, the JVM automatically creates a few Thread
objects and uses them to run different tasks that are needed by it. If you start up VisualVM, you can select the Threads
tab of any JVM process and see the actual threads that are in the JVM. For example, the VisualVM as I started it has 29 live threads. One of them is the thread named main
. This is the one that starts to execute the main
method (surprise!). The main
thread started most of the other threads. When we want to write a multithread application, we will have to create new Thread
objects and start them. The simplest way to do that is new Thread()
, and then calling the start
method on the thread. It will start a new Thread that will just finish immediately as we did not give it anything to do. The Thread
class, as...