notes

How many different ways can you create a thread?

  1. Integrate the Thread class and override the run method. A thread (object) can only execute start() once
  2. Implement the Runnable interface and rewrite the run method inside. You need to wrap the Thread class again before you can call the start method.
  3. Implement callable interface, rewrite call method, return value.
  4. Create a thread pool using ThreadPoolExecutor that implements the Executor interface

Which one do you prefer? Why is that?

In general, the second is the most common.

The Runnable interface has the following benefits:

  1. To avoid the limitation of point inheritance, a class can inherit multiple interfaces.
  2. Suitable for resource sharing

The original

The Thread class is an implementation of the Runnable interface.

② Implement the Runnable interface and rewrite the run method inside.

③ Create a thread pool using the Executor framework. The Executor framework is an implementation of the thread pool provided in JUC.

Call thread start() : starts the thread; Call the corresponding run() method

Thread classes, which inherit from Thread, can call the start method directly to start the Thread (static can also be used for resource sharing). A Thread (object) can only execute start() once, and cannot start a Thread through the run() of the Thread implementation class object.

Classes implementing the Runnable interface need to be wrapped with Thread again before they can call the start method. (Resource sharing is achieved by wrapping three Thread objects around one class object.)

For threads, pay attention to locking and synchronization. (Threaded access to shared resources is prone to thread safety issues)

In general, the second is the most common.

  • The Runnable interface has the following benefits:

*① To avoid the limitation of point inheritance, a class can inherit multiple interfaces.

*② Suitable for resource sharing

/ *

  • Common methods of Thread:

  • 1. Start () : Starts the thread and executes the corresponding run() method

  • 2. Run (): The code to be executed by the child thread goes into the run() method

  • 3. CurrentThread () : static, fetching the currentThread

  • GetName (): Gets the name of this thread

  • 5.setName(): Sets the name of this thread

  • 6.yield(): The thread calling this method releases the execution of the current CPU (probably grabbing the resource itself again)

  • 7. Join (): call the join() method of thread B in thread A, which means: when executing this method, thread A stops executing until thread B finishes executing.

  • Thread A then executes after join()

  • 8. IsAlive (): checks whether the current thread isAlive

  • 9. Sleep (long L): explicitly sleep the current thread for l milliseconds (only catch exceptions because the parent run method does not throw exceptions)

  • Wait () notify() notifyAll() wait() notify() notifyAll()

* Set thread priority (not absolute, but relatively more likely)

  • GetPriority () : Returns the thread priority value

  • SetPriority (int newPriority) : Changes the priority of a thread

* /