1: Programmers can execute multiple threads in a program, each thread completes a function, and executes concurrently with other threads, a mechanism known as multithreading
2: Two ways to implement threading are to inherit Java.lang.Thread or implement the Java.lang.Runnable interface, respectively
The 3:thread class is a class in the Java.lang package, and objects instantiated from this class represent the thread
4: The code that completes the thread's true functionality is placed in the run () method, and when a class inherits the thread class, it can overwrite the Run () method in the class, write the code that implements the thread's functionality into the run () method, and then call the thread the Strat () method in the class executes the thread, which is called the Run () method.
1PackageCom.thread;2/**3* 1: Programmers can execute multiple threads in the program, each thread completes a function, and executes concurrently with other threads, which4* Mechanism is called multithreading5* 2: Two ways to implement threading are to inherit Java.lang.Thread or implement Java.lang.Runnable, respectively.6* Interface7* 3:thread tears when Java.lang a class in a package, objects instantiated from this class represent the thread8* 4: The code that completes the thread's true functionality is placed in the run () method, and when a tear inherits the thread class, it can9* Override the Run () method in this class, write the code that implements the thread's functionality into the run () method, and then call the thread10* The Strat () method in the class executes the thread, which is called the Run () method.11*@authorBiexiansheng12*13*/14PublicClass ThreadTestExtends Thread {//Specifies that the class inherits the thread class1516Privateint count=10;17Publicvoid Run () {//Override the Run () method18WhileTrue){System.out.print (count+ "");//Print Count variable20if (--count==0) {//Subtract the count variable from the loop when it is reduced to 0 o'clock21stReturn;22 }23 }24 }25 public static void main (string[] args) {26 //new threadtest (). Start (); 27 Thread t=new threadtest (); Both methods can implement thread run 28 T.start (); 29 }30}
Examples such as the following
1PackageCom.thread;2/**3* 1: Multi-threaded instance Practice4*@authorBiexiansheng5*6*/7PublicClass ThreadTest1Extendsthread{89//The implementation of multithreading is implemented in the run () method.10PublicvoidRun () {System.out.println (GetName () + "board the Stage");12int count=0;//Define a variable13while (count<=100){System.out.println (GetName () + "board" +count+ "sub-stage");count++;16if (count%10==0){17Try{Thread.Sleep (2000);//Catch exception, hibernate 2 seconds19}Catch(Interruptedexception e) {20//TODO auto-generated Catch block21stE.printstacktrace ();22}//23}24}System.out.println (GetName () + "Go down the stage");26}27PublicStaticvoidMain (string[] args) {28//TODO auto-generated Method StubThread t=NewThreadTest1 ();T.setname ("Mr.thread");31T.start ();32//New ThreadTest1 (). Start ();3334//Implementing the Runnable InterfaceThread t1=New Thread (New Runnabletest (), "ms.runnable");36T1.start ();37}38}3940Class RunnabletestImplementsrunnable{41//After writing the previous sentence, the class name error, indicating the implementation of an interface, it is necessary to implement the method within this interface42@Override43PublicvoidRun () {44//TODO auto-generated Method StubSystem.out.println (Thread.CurrentThread () getName () + "on the stage");46//The Runnable interface implementation GetName must be obtained through the thread's CurrentThread () method47int count=0;//Define a variable48while (count<=100){System.out.println (Thread.CurrentThread () getName () + "Ascend" +count+ "sub-stage");count++;51if (count%10==0){52Try{Thread.Sleep (2000);// catch exception, hibernate 2 seconds 54} catch (interruptedexception e) {55 //< Span style= "COLOR: #008000" > TODO auto-generated catch Block56 E.printstacktrace (); 57} //58 }59 }60 System.out.println ( Thread.CurrentThread (). GetName () + "Go down stage" 61 62 }63 64}
Java implementation multithreading Getting Started