Java Multithreading Summary

Source: Internet
Author: User

Http://www.cnblogs.com/rollenholt/archive/2011/08/28/2156357.html

1. If a class inherits the thread, it is not suitable for resource sharing, but it is easy to realize the resource sharing if the Runnable interface is implemented.

2. Realize the advantages of the Runnable interface: First, for many of the same program code threads to deal with the same resource; second, it avoids the restriction of single inheritance in Java; Thirdly, the code can be shared by multiple threads, the code and data are independent, and the robustness of the program is realized.

The 3.main method is also a thread, in Java, all threads are started at the same time, as to which one executes first, fully depends on who gets the CPU resources first.

4. In Java, each time the program starts running, at least two threads are started, one is the main thread and one is the garbage collection thread. Because every time a class is executed with a Java command, a JVM is actually started. (Each JVM runs, in fact, a process is started in the operating system).

5. The main thread is also likely to end before the child thread ends, and the child thread does not end with the end of the main thread, and it is not affected by the main thread. The main thread in this sentence can be understood as the code inside main. A child thread can be understood as a class that inherits the thread or the. Start () method of a class that implements the Runnable interface.

6.thread.sleep (2000); thread sleeps 2000 milliseconds, which is the output of an object every 2 seconds. The Thread.Interrupt () method is used to break the thread. Thread.setpriority () Sets the priority of the thread (essentially, who gets the CPU's resources first, who executes first). Where thread is the object of the thread class.

7.thread.yield () method, the comity of threads. Let other threads execute first.

8. There are two ways to use threads to troubleshoot synchronization problems. First, use the synchronous code block (synchronized). Second, use the synchronization method. When multiple threads share a resource, synchronization is used, but too much synchronization can lead to a deadlock problem.

Java Multithreading Summary

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.