201671010138 2016-2017 "Java Programming"

Source: Internet
Author: User
Tags thread class

this week we have a good understanding of threading, master two ways to create threads, the first: inherit the thread class,

Rewrite the Run method and write the code to execute in the Run method. The child class object is passed in when the thread object is created. The second type:

Implement the Runable interface and override the Run method. The subclass object that is passed in to the interface when the thread object is created.

When multiple threads start at the same time, each thread will probably have five states, in order to avoid the uncertainty of multi-threaded startup

Usually solved by means of locks and synchronized.

Currentthread.getname (); The method can get the name of the current thread in multithreaded development.

There are some differences between sleep and wait:
1.sleep method must set the time for thread hibernation
The wait method can either be established or not specify the time at which the thread stops.
The 2.sleep method does not release the monitor lock permission when it is executed, and is in a temporary blocking state.
The Wait method releases the lock permission immediately after execution
3.sleep method automatically wakes up after time and continues execution
The wait method waits after execution if it has not been notify or Notifyall wake.
4.sleep methods are not necessarily defined in synchronization
The wait method must be used in the synchronization

We can also use the SetPriority method to prioritize threads.

201671010138 2016-2017 "Java Programming"

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.