=============================================
Source Link: Multithreading (c) Simple use of threads in Java Reprint Please specify the source!
=============================================
In Java, a startup thread is typically started by calling the start () method through thread or its subclasses.
There are two common threads of use: Implementing the Runnable interface and inheriting the thread. Inheriting the thread or using TimerTask's bottom layer still implements the Runnabel interface. Given the single-inheritance limitations of Java, most of the development process is done by implementing the Runnabel interface or Runnbel anonymous classes when using threads.
For example:
Packagecom.zpj.thread.blogTest;/*** Created by Perkinszhu on 2017/8/11 16:42.*/ Public classThreadTest { Public Static voidMain (string[] args) {threadtest threadtest=Newthreadtest (); //all of these startup methods are executed in the Run method .Threadtest.testlambda (); Threadtest.testrunnablewithanonymousrunnable (); Threadtest.testrunnablewithanonymousthread (); Threadtest.testrunnable (); Threadtest.testmythread (); } Public voidTestlambda () {//lambda expressions turn on attributes in thread jdk1.8 NewThread (()-System.out.println ("I am lambda Thread ....")) . Start (); } Public voidTestrunnablewithanonymousthread () {//Anonymous thread class open threads NewThread () {@Override Public voidrun () {System.out.println ("I am threadwithanoymous"); }}.start (); } Public voidTestrunnablewithanonymousrunnable () {//anonymous runnable class open threadThread thread =NewThread (NewRunnable () {@Override Public voidrun () {System.out.println ("I am runablewithanoymous"); } }); Thread.Start (); } Public voidTestrunnable () {//implementing the Runnable interfaceMyrunnable runable =Newmyrunnable (); Thread Thread=NewThread (runable); Thread.Start (); } Public voidTestmythread () {//inherit from ThreadMyThread thread =NewMyThread (); Thread.setname ("MyThread"); Thread.Start (); }}classMyrunnableImplementsRunnable {//implementing the Runnable interface@Override Public voidrun () {System.out.println ("I am myrunnable"); }}classMyThreadextendsThread {//Inherit thread@Override Public voidrun () {System.out.println ("I am mythread!!"); }}
Note that executing line Cheng directly by calling the run () method does not open a new thread, but only calls a common method in the Main method. Using the Start () method opens a new thread execution. The difference is mainly in the former is blocking type, the latter is non-blocking.
For example:
Public voidTeststartandrun () {MyThread thread=NewMyThread (); Thread.setname ("MyThread"); Thread.Start ();//Start starts both asynchronous non-blocking runs while(true{System.out.println ("----" +Thread.CurrentThread (). GetName ()); Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); } } }classMyThreadextendsThread {//Inherit thread@Override Public voidrun () { while(true{System.out.println ("= = =" +thread.currentthread (). GetName ()); Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); } } }}
The results of the operation are as follows:
----Main
= = MyThread
----Main
= = MyThread
= = MyThread
----Main
= = MyThread
----Main
The Thread.Start () is modified to Thread.run ();
= = = main = = = main = = = = Main = = = = Main = = =
This is because the thread name is main because it is the run method called in the main thread, so the print is ===main. and Thread.run (); the loop below the statement is never executed, and the program will continue to loop through the Run method.
So what did the program do when it called the Start method? Look at the underlying implementation.
Public synchronized voidstart () {//verifying the state of a thread if(Threadstatus! = 0) {//The validation here involves a thread that cannot be repeatedly started, and the thread calls start multiple times to throw the exception Throw Newillegalthreadstateexception (); } //Add the thread to the thread groupGroup.add ( This); Booleanstarted =false;//identifies whether the thread started successfully Try{start0 ();//call the native method to start the thread that the method is blocking, and the program waits for it to finish executing the following statement if execution fails and throws an exception directly into finally. started =true;//Modify thread Start state}finally { Try { if(!started) {//start failure, move the thread groupGroup.threadstartfailed ( This); } } Catch(Throwable ignore) {} }}Private native voidStart0 ();//Native method Start thread
With a certain limitation of runnable boot, the execution thread has no return value and cannot catch the exception. In some special cases it is not appropriate to require a thread to return a result. You can then select the callable interface to complete. Callable involves the future model, which comes to the back.
-----End
Multithreading (c) Simple use of threads in Java