I. The concept of threading
CPU execution program, just like a person doing things, the same time you can only do one thing, but such efficiency is too low, when you use the computer, listen to the song can not browse the Web, watching the movie can not download video, you think it is very painful.
So in order to solve this problem, CPU design into a time-sharing process, that is, different times CPU to do different things, so that greatly improve the efficiency, the reason is because the CPU to receive information relative to the speed of the person, much faster! That is, you listen to the song while browsing the web, it looks like the music player and browser two programs at the same time work, in fact, they are tick in the work, that is, the player work a while, the browser work for a while, so cross-executing, but you accept the information is very slow, so it does not feel it.
So many threads, and more than one program is the same reason, multithreading is a program inside multiple sub-task ticks execution. For example: QQ video chat, you can watch the video, the side of the message, this is two threads.
PS: In addition, many people explain the difference between the process and the thread , with a lot of abstract concepts, say the same, obscure, in my opinion, the process and the thread are running programs , but the process is running a separate program (such as: Qq,lol, QQ audio and video), and the thread is a separate program inside the running sub-program (such as: QQ in the first message, send and receive video, download files, etc.), if the thread out, is also a process. One reason for comparing these two comparisons is that they have another common denominator: they are all time-sharing . That is, the process is a different program of time-sharing execution (such as the use of QQ can play LOL), and the thread is a program within a number of sub-program execution (such as chatting with QQ can accept and play the other side of the camera video, and also can download pieces).
Two. How threads in Java are implemented
In Java, there are two ways of implementing Multithreading:
1. Inheriting the thread class
Details: After inheriting the thread class, rewrite the Run () method, knock the code that will implement multithreading into the Run () method, and then instantiate the class in the main function (that is, the class that previously inherited and rewritten the run () method) and invoke the inherited start () method. Starting from where the method is called, the CPU executes the program code in run () using the idle time period.
1 Importjava.lang.*;2 Public classTestthreadextendsthread{3 4 Public voidrun () {5 System.out.println (Thread.CurrentThread (). GetName ());6 }7 8 9 Public Static voidMain (string[] args) {TenThread TT =NewTestthread (); One Tt.start ();//Here Another thread is started A System.out.println (Thread.CurrentThread (). GetName ()); - } - the}
Operation Result:
Mainthread-0
2. Implement the Runable interface
Details: Thread This class, constructed with two methods: Thread () and thread (Runnable target).
Here we introduce the second construction method, thread (runable target), which is an interface that defines a run () method, which can refer to this interface for a class to implement the run () method, Put the code that requires multi-threaded execution into the run () method, and then call the class that implements the Runnable interface with the second construction method. I have to say the interface is an artifact.
1 Importjava.lang.*;2 Public classTestthreadImplementsrunnable{3 4 Public voidrun () {5 System.out.println (Thread.CurrentThread (). GetName ());6 }7 8 9 Public Static voidMain (string[] args) {TenThread TT =NewThread (NewTestthread ()); One Tt.start (); A System.out.println (Thread.CurrentThread (). GetName ()); - } - the}
Operation Result:
Mainthread-0
3. The difference between the two approaches
- Classes in Java can only be single-inheritance, so when a class is already a subclass of inheriting classes and adding multithreading, the second way to implement the Runnable interface
- Because the two are constructed differently, the first is to create a new object each time to implement multi-threading, and the second way enables multiple threads to share the resources of an object (a class that implements the Runnable interface) , which can be used depending on the situation.
For example: Simulation of a ticket sales process, a total of 100 tickets, three people tickets respectively.
1 Importjava.lang.*;2 Public classTestthreadImplementsrunnable{3 Private intnum = 100;4 Public voidrun () {5 while(num-->=0)6System.out.println (Thread.CurrentThread (). GetName () + ': ' +num);7 return;8 }9 Ten One Public Static voidMain (string[] args) { ATextthread TT =NewTestthread (); - NewThread (TT). Start (); - NewThread (TT). Start (); the NewThread (TT). Start (); - System.out.println (Thread.CurrentThread (). GetName ()); - } - +}
Run partial results:
thread-2:91thread-2:90thread-2:89thread-2:88thread-2:87thread- 1:86thread-1:84thread-1:83
Three. Some other methods in the thread class
Static thread Curruntthread () returns a reference to the currently executing thread object
String GetName () returns the thread name
void Join () inserts the thread at the current position
void join (Long Millis) inserts the thread at the current position to execute Millis milliseconds before continuing the thread
2016-08-26 01:16:19
Java Learning notes 2--multithreading