Java Learning Multi-threading

Source: Internet
Author: User

The thread class in 1.java represents a thread and two ways to create a thread:
1) Create a subclass of thread, override the Run method, and need to invoke the thread's start () method to start the thread;
2) Create the implementation class of the Runnable interface and implement the Run method in the interface;

It is generally recommended to use the second method to implement multi-threading, because the first is to realize the sharing of multi-threaded resources, and the subclass that implements multithreading can inherit other parent classes.
2.String threadname= Thread.CurrentThread (). GetName (): Gets the name of the current execution thread;


3. How to implement the Runnable interface:
3.1: First create an implementation class that implements the Runnable interface: The Run method must be implemented
3.2 Create 3.1 The corresponding Runnable interface implementation class object;
3.3: Create the Thread object, using the thread (Runnable target) constructor to create
3.4 Calling the Thread class start () method to start a thread


4. Several methods related to thread life cycle:
4.1 Yield () method, if the current thread calls the method, the thread is run from the execution state to the operational state

4.2sleep (int min) hibernate, which causes the current thread to hibernate for a period of time () in milliseconds
4.3 Join () method, calling another thread method in one thread, will block the current thread, wait for the other thread to finish, and then enter the executable state

4.4 Interrupt () method, which will unblock the blocking state of the thread

The terminated thread calls the Start method again and throws an exception

4.6 isAlive () method to determine whether a thread is alive (operational or operational)


5. Thread-Safe issues:
5.1 Understanding and writing thread Unsafe instance code: multiple threads accessing a shared resource
5.2 Troubleshooting thread Safety with synchronized code blocks: need to be in a synchronized code block
Refer to a Common object

6. About inter-thread communication:
6.1: Related methods: Wait (), notify (), Notifyall ()
6.2 These methods are called in the synchronous Method (synchronized)
How to use two threads to cross print A to C, to achieve alternating effects??

Java Learning Multi-threading

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.