转载请注明:http://blog.csdn.net/UniKylin/article/details/45016117
1. The difference between threads and processes
Parallel : Multiple tasks are performed simultaneously at the same time, for example a multi-core computer can be interpreted as parallel
concurrency : From a microscopic point of view, multiple tasks preempt a CPU to perform its own tasks, taking turns performing tasks, but without radically improving execution efficiency when encountering resource conflicts. But it improves the efficiency of CPU usage.
A picture of a previous time on GitHub can be very good at explaining the concept of the above image
Two ways to create threads in 2.Java
1. First approach : Direct inheritance of thread:
Public class Dog extends Thread { //Private variable Private intNumber//Initialize multiplication table upper number Public Dog(intNumber) { This. Number = number; }//Rewrite logical method @Override Public void Run() {//Print multiplication table for(inti =1; I <= number; i++) { for(intj =1; J <= I; J + +) {System.out.print (j +" * "+ i +" = "+ I * j +"\ T"); } System.out.println (); } } Public Static void Main(string[] args) {Dog Dog =NewDog (9); Dog.start ();//Start thread print multiplication table}}
2. Second approach : How to implement the Runnable interface
Public class Cat implements Runnable { Private intNumber Public Cat(intNumber) { This. Number = number; }//Rewrite logical method @Override Public void Run() {System.out.println ("Current Thread:"+ Thread.CurrentThread ()); for(inti =0; I <= number; i++) { for(intj =0; J <= I; J + +) {System.out.print (i +" * "+ j +" = "+ I * j +"\ T"); } System.out.println (); } } Public Static void Main(string[] args) {Thread task =NewThread (NewCat (9)); Task.start (); }}
You can find that the first way to inherit the thread is no longer inherited from the other parent classes, so it is not very extensible for the program, but you can access information about the current thread through this. The second way to inherit the Runnable
interface, from a design point of view is more flexible, Java is not support multiple inheritance but support the implementation of multiple interfaces, so you can overcome this shortcoming, if you want to access the thread information needs to use the Thread.currentThread();
method.
3. Source Code Analysis
Public class Thread implements Runnable { //Associated runnable interface PrivateRunnable Target;//Thread logic method Public void Run() {if(Target! =NULL) {Target.run (); } }}//thread interface Public interface Runnable { //define abstract thread logic methods, Public Abstract void Run();}
The above thread class and runnable interface are two of the most important core parts of the Java JDK, inheriting that thread belongs to the extension of the system class on the JDK itself, and implementing the Runnable interface is a standard implementation. Either way, the JVM loads the threads as they are started, except in a different way. Which method is better, of course, the second way is more elegant and flexible in terms of design.
Java concurrent Programming-two ways to create threads and differences