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