我要评分
获取效率
正确性
完整性
易理解

Common Threads and Precautions in Java Programming

Thread Creation Methods

  • Extending the Thread class: Extend the Thread class and override the run() method to define the concurrent execution process. Create an instance of this class and use the start() method to start the thread.
  • Implementing the Runnable interface: Implement the Runnable interface and the run() method to define the concurrent execution process. Create an instance of this class, pass this instance as a parameter to the Thread constructor to create a Thread class, and use the start() method to start the thread.

Thread States

  1. After a thread is created using the new operator, it enters the New state (initial state).
  2. After the start() method is called, the thread transitions from the New state to the Runnable state (ready state). The start() method is called in the main() method (Running state).
  3. Once execution concludes, the thread enters the Dead state,
  4. in which it is eligible for garbage collection and cannot be restarted.
  5. If the thread calls the yield() method during running, the thread transitions from the Running state to the Runnable state.

Thread State

Description

New

  • After a program creates a thread using the new operator, the thread is in the New state and has not yet started.
  • Upon calling the start() method on the thread object, the thread is started and transitions to the Runnable state.

Runnable

When a thread is in the Runnable state, the thread is ready and waiting to acquire CPU resources.

Running

Once the thread acquires CPU resources, it transitions to the Running state and begins executing the thread body, which is the content defined within the run() method.

NOTE:
  • If the system has only one CPU, only a single thread can be in the Running state at any time.
  • In a dual-core system, two threads can occupy the Running state simultaneously. However, if the number of threads exceeds the number of cores, multiple threads will continue to execute on the same CPU through a rotational mechanism.
  • When a thread starts to run, if it is not completed immediately, it cannot remain in the Running state. The thread may be interrupted to provide other threads with an opportunity to run. Such thread scheduling strategies are determined by the underlying platform. On platforms utilizing a preemptive scheduling strategy, the system allocates a small duration of time to each executable thread to process tasks. Once this duration, known as a time slice, is exhausted, the system preempts the CPU resources from the current thread to allow other threads to run.
  • Calling the yield() method can change the thread state from Running to Runnable.

Block

A thread enters the Block state in the following situations:

  • The thread calls the sleep() method, relinquishing its allocated CPU resources.
  • The thread calls a blocking I/O method (such as a console input method); the thread remains blocked until this method returns.
  • When the executing thread is blocked, other threads are granted the opportunity to execute. Once the thread is unblocked, it transitions to the Runnable state rather than resuming the Running state immediately.

Dead

Once the run() method completes execution, the thread enters the Dead state.

NOTE:

Do not call the start() method on a Dead thread which cannot be executed again. Attempting to do so will cause the system to throw an IllegalThreadStateException.