1 Overview of multi-Threading 1.1 processes
- Is the program that is running. The system is a separate unit for resource allocation and scheduling. Each process has its own memory space and system resources.
What is the meaning of more than 1.2 processes?
- A single-process computer can do only one thing, and we now have a computer to do multiple things. For example: Play the game while listening to music.
- Today's computers support multiple processes, can perform multiple tasks in a single time period, and can increase CPU usage.
1.3 Threads
- Multiple tasks can be performed within a process, and each of these tasks can be viewed as a thread.
- A thread is the execution unit (execution path) of a process.
- Single thread: If the program has an execution path.
- Multithreading: If the program has more than one execution path.
1.4 What is the meaning of multi-threading?
- Multithreading exists, not to improve the execution speed of the program. In fact, to improve application usage.
- The implementation of the program is actually in the grab CPU resources, that is, the CPU execution right.
- Multiple processes are robbing this resource, and one of those processes has a higher chance of grabbing CPU execution if the execution path is more.
- The execution of a thread is random.
1.5 How Java programs work
- The Java command launches the Java Virtual machine and starts the JVM, which is equivalent to initiating a process. The process automatically starts a main thread and then calls the main method of a class by the main thread, so the main method runs in the main thread.
- The JVM startup starts at least the garbage collection thread and the main thread, so it is multithreaded.
2 Multi-Threading Implementation Scenario 2.1 Multi-Threading implementation Scenario One
- Steps:
- ① create a class to inherit the thread class.
- ② the subclass to rewrite the run () method.
- ③ the object that created the subclass.
- ④ Start Thread
PackageCom.xuweiwei;/*** Multithreading implementation: * Because threads are dependent on processes, we should create a process first. * and the process is created by the system, so we should call the system function to create a process. * Java is not directly with the system function, so we have no way to directly implement multithreaded programs. * However, Java can be called C or C + + written programs to implement multithreaded programs. * from C or C + + to invoke the system function creation process, then by Java to invoke such things, and then provide some APIs for us to use. In this way, we can implement multithreaded programs. */ Public classMyThreadextendsThread {@Override Public voidrun () { for(inti = 0; I < 10; i++) {System.out.println (Thread.CurrentThread (). GetName ()+"= " +i); } }}
Package Com.xuweiwei; Public class mythreadtest { publicstaticvoid main (string[] args) { New MyThread (); T1.start (); New MyThread (); T2.start (); }}
2.2 Gets the name of the thread object and sets the name of the thread object
- Method: Returns the name of the thread
Public Final String GetName ()
- Method: Set the name of the thread
Public Final void setName (String name)
- Method: Returns a reference to the currently executing thread object
Public Static Thread CurrentThread ()
- Example: Non-parametric construction method +setname
package Com.xuweiwei; public class MyThread extends public run () { for (int i = 0; i < i++) {Sy Stem.out.println (Thread.CurrentThread (). GetName () + "=" + i); } }}
Package Com.xuweiwei; Public class mythreadtest { publicstaticvoid main (string[] args) { New MyThread (); New MyThread (); // Set the name of the thread T1.setname ("T1"); T2.setname ("T2"); T1.start (); T2.start (); }}
- Example: A method for constructing a parameter
PackageCom.xuweiwei; Public classMyThreadextendsThread { PublicMyThread () {} PublicMyThread (String name) {Super(name); } @Override Public voidrun () { for(inti = 0; I < 10; i++) {System.out.println (Thread.CurrentThread (). GetName ()+"= " +i); } }}
Package Com.xuweiwei; Public class mythreadtest { publicstaticvoid main (string[] args) { New MyThread ("T1"); New MyThread ("T2"); T1.start (); T2.start (); }}
- Example: Get the name of the thread object where the Main method resides
Package Com.xuweiwei; Public class mythreadtest { publicstaticvoid main (string[] args) { System.out.println (Thread.CurrentThread (). GetName ());} }
2.3 Control of the thread
Public Static void sleep (long Millis) throws interruptedexception
- Method: Join thread, wait for the thread to terminate
Public Final void throws Interruptedexception
- Method: Thread comity pauses the current executing thread object and executes other threads.
Public Static void yield ()
Public Final void Setdaemon (boolean on)
- Method: Middle Thread thread
Public void Interrupt ()
PackageCom.xuweiwei; Public classMyThreadextendsThread {@Override Public voidrun () { for(inti = 0; I < 10; i++) { Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); } System.out.println (Thread.CurrentThread (). GetName ()+"= " +i); } }}
Package Com.xuweiwei; Public class extends Thread { @Override publicvoid run () { for (int i = 0; I < 100; i++) { + "=" + i); }}}
PackageCom.xuweiwei; Public classMythreadtest { Public Static voidMain (string[] args) {MyThread T1=NewMyThread (); MyThread T2=NewMyThread (); MyThread T3=NewMyThread (); T1.setname ("Liyuan"); T2.setname ("Li Jiancheng"); T3.setname ("Li Shimin"); T1.start (); Try{t1.join (); } Catch(interruptedexception e) {e.printstacktrace (); } t2.start (); T3.start (); }}
Package Com.xuweiwei; Public class extends Thread { @Override publicvoid run () { for (int i = 0; I < 100; i++) { + "=" + i); Thread.yield (); }}}
PackageCom.xuweiwei; Public classMythreadtest { Public Static voidMain (string[] args) {MyThread T1=NewMyThread (); MyThread T2=NewMyThread (); MyThread T3=NewMyThread (); T1.setname ("Liyuan"); T2.setname ("Li Jiancheng"); T3.setname ("Li Shimin"); T1.start (); T2.start (); T3.start (); }}
Package Com.xuweiwei; Public class extends Thread { @Override publicvoid run () { for ( int i = 0; I < 100; i++) { + "=" + i); Thread.yield (); }}}
PackageCom.xuweiwei; Public classMythreadtest { Public Static voidMain (string[] args) {MyThread t2=NewMyThread (); MyThread T3=NewMyThread (); T2.setname ("Li Jiancheng"); T3.setname ("Li Shimin"); T2.setdaemon (true); T3.setdaemon (true); T2.start (); T3.start (); Thread.CurrentThread (). SetName ("Liyuan"); for(inti = 0; I < 3; i++) {System.out.println (Thread.CurrentThread (). GetName ()+"= " +i); } }}
2.4 The life cycle of a thread
- NEW: Create thread object.
- Ready: There is an executive qualification, but no enforcement right.
- Run: Have the execution qualification, have the execution right.
- Blocking: The thread is in that state due to some actions. No executive qualification, no executive right.
- Death: The Thread object object becomes garbage and waits to be recycled.
2.5 Multi-Threading Implementation mode two
3 thread scheduling and line programming
4 Thread life cycle
5 dead Lock
6 Inter-thread communication
7 Use of Timers
Java Fundamentals 22