(i) thread class
1. Structure
Java.lang.Object
|---java.lang.Thread
2. Two ways to create a thread
(1) One method is to declare a class as a subclass of thread , which should override the run method of the thread class
Class Primethread extends Thread {long minprime; Primethread (Long minprime) {this.minprime = Minprime; } public void Run () {///compute primes larger than minprime ...}} Primethread p = new Primethread (143);p. Start ();
(2) Another way to create a thread is to declare a class that implements the Runnable interface , and then implement the Run method with that class . You can then assign an instance of the class that is passed and started as a parameter when the thread is created
(3) This method provides an extension method for implementing multi-threading to implement the class that has already implemented the inheritance, and implements the interface
Class Primerun implements Runnable {long minprime; Primerun (Long minprime) {this.minprime = Minprime; } public void Run () {///compute primes larger than minprime ...}} Primerun p = new Primerun (143); new Thread (P). Start ();
Code Listing 1: The first way to create a thread
Mythread class
1. Inherit the Thread class public class MyThread extends Thread{private String threadname;public MyThread () {super ();} Provides a method for constructing a parameter, passing the name of the parent class directly to the constructor of a thread whose name is public MyThread (String threadname) {super (threadname); this.threadname = ThreadName;} 2. Rewrite the Run method @overridepublic void run () {for (int i=0;i<5;i++) {System.out.println (This.getname () + ": Hello:" +i);}}
Main
public class Main {public static void main (string[] args) {//no_parameter_construction_method (); threadname_ Construction_method ();} /** * 1. Call the parameterless construction method to create the thread object and set the thread name * */public static void No_parameter_construction_method () {//3. Create an instance object for the thread Mythread my1= New MyThread (); MyThread my2=new MyThread ()///4. You can set the name of the thread, do not set the default name used by the JVM (thread-n) my1.setname ("Thread 1"), My2.setname ("Thread 2");//5. The start thread//Call run () is simply the invocation method//The calling thread should use the start () method My1.start (); My2.start (); System.out.println ("End");} public static void Threadname_construction_method () {//3. Create the instance object of the thread MyThread my3=new MyThread ("Thread 3"); MyThread my4=new MyThread ("Thread 4");//4. Start thread My3.start (); My4.start (); System.out.println ("End");}}
Code Listing 1: The second way to create a thread
Ourthread class
1. Implement Runnable interface public class Ourthread implements runnable{//2. Override the Run method @overridepublic void run () {for (int i=0;i<10; i++) {//3. Gets the current thread name System.out.println (Thread.CurrentThread (). GetName () + ": Hello:" +i);}}
Main
public class Main {public static void main (string[] args) {//These 2 method calls do not wait for the previous method to be finished, the next method starts, but does not block directly begins CreateThread (); Createnamethread ();} public static void CreateThread () {//4. Create an Implementation interface class object//5. Create thread Ourthread ot1=new ourthread (); Thread t1=new thread (OT1); Ourthread ot2=new Ourthread (); Thread t2=new thread (OT2); T1.start (); T2.start (); System.out.println ("End");} public static void Createnamethread () {///4. Create an Implementation interface class object//5. Create a thread that can take a name without using the JVM default assigned name Ourthread ot1=new ourthread (); Thread T1=new thread (OT1, "thread"); Ourthread ot2=new Ourthread (); Thread T2=new thread (ot2, "thread"); T1.start (); T2.start (); System.out.println ("End");}}
2 ways to create threads with "thread"