The Java Thread Class

Java provides the java.lang.Thread class for explicitly creating and controlling threads. To use threads, first create a subclass of Thread. This subclass must include a run method; the code within the run method performs the thread's task. Each instantiation of this subclass corresponds to a single thread. To start the thread, the controlling program invokes the java.lang.Thread.start method. Invoking start causes the thread's run method to be invoked. This is done right away; we do not wait for another thread to complete the run method. In this way, multithreaded execution is achieved. The thread is implicitly stopped as soon as the run method terminates. One important method in the Thread class is sleep: this causes the thread to cease execution for a specified number of milliseconds.

No comments:

Post a Comment