A thread is a thread of execution in a program. A Java virtual machine allows an application to run multiple execution threads concurrently.
Each thread has a priority, and the execution of the high-priority thread takes precedence over the low-priority thread. Each thread can or cannot be marked as a daemon. When code running in a thread creates a new Thread
object, the initial priority of the new thread is set to the priority of the thread being created, and the new thread is the daemon only if the creation thread is the daemon thread.
When a Java virtual machine is started, it usually has a single non-daemon thread (which typically invokes a method of a specified class main
). Java virtual opportunities continue to execute threads until any of the following conditions occur:
The Runtime
method of the class was called exit
, and the security manager allowed the exit operation to occur.
All threads that are not daemon threads have stopped running, either by returning from a call to the Run method or by throwing an exception that is propagated run
outside the method.
There are two ways to create a new thread of execution:
1): One method is to declare the class as Thread
a subclass. The subclass should override Thread
the method of the class run
. You can then assign and start an instance of the subclass.
public class demothread1 {/** * @author Shepherd's queen * @param args * date:2015-10-15 */public static void main (String[] args) { The main thread of the //is responsible for executing the Main method Speakdog dog = new speakdog (); // Creating Threads speakpig Pig = new speakpig (); //create thread Dog.start (); // start thread Pig.start (); // startup thread for ( int i=1;i<=10;i++) {System.out.print ("sheep " +i+ " ");}}} Subclasses of the Public class speakdog extends thread { // thread class @overridepublic Void run () {for ( int i=1; i<=5; i++) {System.out.print ("dog " +i+ " ");}}} public class speakpig extends thread {@Overridepublic void run () {for ( int i=1;i<=7;i++) {System.out.print ("Pig " + i + " ");}}} The above JVM lets dog , pig , main threads use CPU resources in turn. The JVM ends the execution of the Java program only if all threads in the program are finished. Insufficient: the above-mentioned programs are different in that the computer runs or runs repeatedly on the same computer, because the output results in the usage of the current CPU resources.
2) Another way to create a thread is to declare a class that implements Runnable
the interface. The class then implements the run
method. You can then assign an instance of the class that is Thread
passed and started as a parameter at creation time.
Thread (Runnable target)
|
Assigning a new Thread object |
|
Public class demothread2 {public static void main (String[] args) { thread speakdog; //the thread thread speakpig; //with thread The thread speakdog dog; // dog is the target object with thread declaration speakpig pig; // pig is the target object Dog = new speakdog (); //creates the target object Pig = new speakpig (); // Create target object Speakdog = new thread (dog); //Create thread Its target object is Dogspeakpig = new thread (pig); //Create thread Its target object is Pigspeakdog.start (); //start thread Speakpig.start (); //start thread for ( int i=1; i<=10;i++) {System.out.print ("sheep " + i +" ");}} public class speakdog implements runnable {@Overridepublic void run () {for ( int i=1; i<=5; i++ ) {System.out.print ("dog " + i + " " );}}} Public class speakpig implements runnable {@Overridepublic void run () { for ( int i=1; i<=7; i++ ) {System.out.print ("Pig " + i + " ") ;}}}
Threads can share the same memory units (including code and data) and use these shared units for data exchange, real-time communication and necessary synchronization operations.
When you create a thread for the Runnable target construction method, when it comes to using CPU resources, the target object automatically calls the run () method in the interface, so for a thread that uses the same target object, The member variables of the target object are naturally the data units shared by these threads.
In addition, the class that creates the target object can be a specific subclass if necessary, so it is more flexible to use the Runnable interface than a subclass that uses thread.
public class MyThread extends Otherclass implements Runnable {public void run () {System.out.println ("Mythread.run ()"); }}//when a runnable target parameter is passed to the thread, the thread's Run () method calls Target.run (), referencing the JDK source code: public void Run () {if (target! = null) { Target.run (); } }
Learn note thread class and thread creation in 2:java