Analysis of Java multithreading usage points

  • 2020-05-19 04:53:09
  • OfStack

Multithreading details

What are the similarities and differences between the sleep method and the wait method?

Similarities:

Leave the thread frozen.

Difference:

sleep must specify a time
wait may or may not specify a time

When sleep time is up, the thread is temporarily blocked or running
If there is no time, wait must be awakened by notify or notifyAll

sleep must not be defined in synchronization
wait must be defined in synchronization

Are defined in sync
sleep puts the execution power, does not put the lock
wait puts the execution power, puts the lock


syschronized(obj)
 {
   wait();// 0 1 2 
   code...
 }
 syschronized(obj)
 {
  notifyAll();// 3
  code...
 }

How threads stop

stop method

The stop method is out of date, and looking at the description reveals that there are other solutions.
Thread end: when the thread task code is finished, the run method ends
How the run method ends ?
By defining the loop

Note: the 10000 thread is frozen in the task. Can he still judge the tag?
An interrupt state is not a stop thread.
interrupt interrupt status
If the target thread is waiting for a long time, the interrupt method should be used to interrupt the wait
An interrupt is not a stop to the thread.
The function of interrupt is to clear the frozen state of the thread and restore the thread to the running state (make the thread qualified to execute cpu again).
Because the time is mandatory, an exception InterruptedException will occur. You can catch the exception in catch.
In exception handling, change the flag to end the loop and end the run method.

Daemon thread

Daemon threads: also known as background threads, previously created are foreground threads.
As long as the thread calls setDaemon(true); You can mark threads as daemon threads.
Foreground and background threads are all running like 1, and get the execution right of CPU.
Only at the end it was different.
The foreground thread ends with the run method, and the thread ends.
The background thread can also end with the run method, the thread ends, and then there's another case,
When all the foreground threads in the process are finished, whatever state the background thread is in, it will end and the process will end.
Process termination depends on the foreground thread.

Thread priority

Thread priority: numbered, 1-10
One of the default initial priority when 5 most obvious 3 priority 1,5,10.
setPriority(Thread.MAX_PRIORITY);

Thread group

Thread group: ThreadGroup: you can specify the thread group to which the new thread object belongs through the constructor of Thread.
The advantage of thread groups, can be the same group of threads, the operation of 1.
By default, all belong to the main thread group.

Anonymous inner class


Runnable rn = new Runnable() {
  public void run() {
  }
};

// The code above is equivalent to 

class Anomymous implements Runnable {
  public void run() {
  }
}
Runnable rn = new Anomymous();

 

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: