Java Multithreading Complete version of the basic knowledge (Kai Kaishun from thick to thin series) 1. Foreword
Threads are a very important concept in modern operating systems, the multithreading is powerful, the Java language provides good support for threading, and we can easily create multiple threads using the thread class provided by Java. Thread is not difficult, I have studied before the foundation, in this done a collation, this article mainly refers to the Java research organization published J2SE Advanced and Zhang Xiaoxiang:-java Employment Training teaching materials these two books 2. Overview 2.1 Threads what
is mainly the difference between thread and process, no longer elaborated here, self-search online
Why use Threads: the operating system switches multiple threads faster than the scheduling process, can not be shared between processes, communication problems. It is convenient to exchange data between threads because of shared data
Here's an example to explain how multithreading and single-threaded
A single thread example
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22-23 |
package firsttread; /** * @author zhaikaishun * */public class treaddemo1 { public static void main (String[] args) { new testthread (). run (); //will execute this code all the time while (true) { system.out.println ("main thread is running"); } } class testthread{ //here does not inherit thread class public void run () { while (true) { System.out.println ("Testthread is here run");/will always perform } } } |
appears after running
Testthread is here run
Testthread is here run
Testthread is here run
Testthread is here run
Testthread is here run
...
Analysis: This is a single-threaded, will be in order, will only execute the Testthread class method
B Multithreaded Example