Java Multithreading Usage Essentials Analysis _java

Source: Internet
Author: User

Multithreading detail Issues

The similarities and differences between the sleep method and the wait method?

Same point:

Put the thread in a frozen state.

Different points:

Sleep must specify time
Wait can specify a time, or you can not specify a time

Sleep time to, thread in temporary blocking state or run state
Wait, if there is no time, must be awakened through notify or Notifyall.

Sleep is not necessarily defined in sync
Wait must be defined in sync

are defined in the synchronization
Sleep put execution right, do not put lock
Wait to put the execution right, put the lock

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

How the thread stops

Stop method

The Stop method is out of date, looking at the description found that there are other solutions.
Thread end: Just let the thread task code finish and the Run method ends
How does the Run method end?
By defining loops

Note: If the thread is frozen in the task, can he still judge the mark?
The so-called interrupt state is not a stop thread.
Interrupt Interrupt status makes
If the target thread waits a long time, you should use the interrupt method to break the wait
The so-called interrupt is not a stop thread.
The function of the interrupt is to clear the frozen state of the thread, to restore the thread to its running state (and to have the thread regain the CPU's execution).
Because it is mandatory so there will be an exception interruptedexception, you can catch the exception in Catch,
In exception handling, change the tag to end the loop, and let the Run method end.

Daemon Threads

Daemon Thread: It can also be understood as a background thread, which was previously created as a foreground thread.
As long as the thread calls Setdaemon (true), the thread can be marked as a daemon thread.
The foreground background thread runs all the same, obtaining execution of the CPU power.
It's only a little different at the end.
The foreground thread ends with the Run method, and the thread finishes.
Background threads can also end with the Run method, the thread ends, and in another case,
When all foreground threads in a process are finished, the process ends, regardless of what state the background thread is in.
The process end depends on the foreground thread.

Thread Priority

Priority of the thread: Digital ID, 1-10
Where the default initial priority is 5 of the most obvious three priority 1,5,10.
SetPriority (thread.max_priority);

Thread Group

Thread Group: Threadgroup: The thread group that the new thread object belongs to can be identified through the thread's constructor.
The benefits of a thread group, you can perform a unified operation on multiple threads of the same group.
The default belongs to the main thread group.

Anonymous inner class

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

The above code 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 for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.