Multithreading
Thread: A thread is an execution unit in a process that is responsible for the execution of a program in the current process, with at least one thread in a process. There can be multiple threads in a process, and this application can also be called a multithreaded program.
Single-threaded: that is, if more than one task can be executed sequentially. When the previous task finishes executing, the next task begins. For example, to Internet cafes, cafes can only let a person online, when this person under the machine, the next person to the Internet.
Multithreaded: That is, if more than one task can be executed at the same time. For example, to Internet cafes, cafes can allow many people at the same time Internet.
Program Operating principle
1. Time-sharing scheduling
All threads take turns using the CPU usage, averaging the time each thread consumes the CPU.
2. Preemptive scheduling
Prioritize high-priority threads to use the CPU, and if the threads have the same priority, then randomly select one (thread randomness), which Java uses for preemptive scheduling.
Main thread
After the JVM is started, there must be an execution path (thread) starting from the main method and executing until the main method ends, which is called the main thread in Java.
Thread class
Thread is the thread of execution in the program. A Java virtual machine allows an application to run multiple execution threads concurrently.
Common methods:
There are two ways to create a new thread of execution:
1. One method is to declare a class as a subclass of Thread. The subclass should override the Run method of the Thread class. Create the object to open the thread. The Run method is equivalent to the main method of another thread.
2. Another way is to declare a class that implements the Runnable interface. The class then implements the Run method. It then creates a subclass object of runnable, which is passed into the constructor method of a thread, and the thread is opened.
Create thread mode one inherits the thread class
Steps to create a thread:
1 defines a class that inherits the thread.
2 override the Run method.
3 Creating a Subclass object is creating a thread object.
4 calls the Start method, opens the thread and lets the thread execute it, and tells the JVM to call the Run method.
Custom threading Classes
Packagecom.oracle.demo01; Public classMyThreadextendsThread { PublicMyThread () {} PublicMyThread (String name) {//invokes the constructor of the parent class's string argument, specifying the name of the thread Super(name); } @Override
//Override the Run method to finish editing the thread's execution Public voidrun () {System.out.println (Super. GetName ()); for(inti=0;i<100;i++) {System.out.println (i+"..."); } }}
Test class:
Packagecom.oracle.demo01; Public classDemo01 { Public Static voidMain (string[] args) {//Main Thread /*for (int i=0;i<100;i++) {System.out.println ("main1" + "..." +i); }*/ //Create a new threadMyThread MT =NewMyThread (); //naming the threadMt.setname ("Invincible"); //Open new ThreadMt.start (); //the second way of naming, using the method of parameter construction to transmit the valueMyThread m =NewMyThread ("Chic"); M.start (); //NULL parameter constructs create custom thread objectsMyThread M1 =NewMyThread (); M1.start (); for(intj=0;j<100;j++) {System.out.println ("Main2" + "..." +j); }//returns a reference to the currently executing thread objectThread d =Thread.CurrentThread (); //Print the name of the thread returnedSystem.out.println (D.getname ()); }}
Results Demo:
Create thread mode-Implement Runnable interface
The steps to create the thread.
1, the definition class implements the Runnable interface.
2. Overwrite the Run method in the interface.
3. Create the object of the thread class
4. Pass the subclass object of the Runnable interface as a parameter to the constructor of the thread class.
5. Call the Start method of the thread class to open the thread.
Custom Thread execution Task class
Public class Implements runnable{ // Define the Run method logic to be executed by the thread @Override public void Run () { for (int i = 0; i < i++ ) { System.out.println ( /c16> "My thread: Executing! "+i); }}}
Test class:
Public classDemo02 { Public Static voidMain (string[] args) {//To create a thread execution target class objectRunnable Runn =Newmyrunnable (); //passing the subclass object of the Runnable interface as a parameter to the constructor of the thread classThread thread =NewThread (Runn); Thread thread2=NewThread (Runn); //Open ThreadThread.Start (); Thread2.start (); for(inti = 0; I < 10; i++) {System.out.println ("Main thread: Executing!" "+i); } }}
inherit the thread class and implement the Runnable interface differences :
Implements the Runnable interface, avoids inheriting the single inheritance limitation of the thread class. Override the Run method in the Runnable interface to define the thread task code in the Run method.
Creates an object of the thread class, and only objects that create the thread class can create threads. The thread task has been encapsulated into the Runnable interface's Run method, and the Run method belongs to the subclass object of the Runnable interface, so the subclass object is passed as a parameter to the thread's constructor, so that the thread object can be created with the task of defining the thread to run.
Benefits of achieving runnable :
Implementing the Runnable interface avoids the limitations of single inheritance
Implement the Runnable interface, separate the thread tasks into objects, and the type is the Runnable interface type. The Runnable interface decouples thread objects and thread tasks.
Anonymous internal classes for threads are used
Method 1: When you create a thread object, directly override the Run method in the thread class
New Thread () { @Override publicvoid run () { // TODO auto-generated method Stubfor (int i=0;i<50;i++) { System.out.println ("Run" + "..." +i);}} . Start ();
Method 2: Implement the Runnable interface using an anonymous inner class and re-runnable the Run method in the interface
New Runnable () { publicvoid run () { for (int x = 0; x < 40; x + +) { System.out.println (Thread.CurrentThread (). GetName ()+ "... Y .... "+ x);}} ; New Thread (R). Start ();
Simplified:
New New Runnable () { publicvoid run () { for (int x = 0; x < 4 0; x + +) { System.out.println (Thread.CurrentThread (). GetName ()+ "... Y .... "+ x);}} ). Start ();
Thread Execution Status:
Multithreading, thread class, runnable interface