Java multi-Thread implementation method code explanation, multi-thread Thread
I have briefly introduced the use of java multithreading. I have already used Thread classes and Runnable classes. In order to better understand multithreading, this article will analyze Thread in detail.
Start ()
Let's take a look at the introduction of this method in the API:
The Java Virtual Machine calls the run method of the thread.
The result is that two threads run concurrently. The current thread (returned from the call to the start method) and the other thread (executing its run method ).
It is invalid to start a thread multiple times. In particular, the thread cannot be restarted after it has completed execution.
The start method is used to start the thread and realize multi-threaded running. In this case, you do not need to wait until the execution of the run method body code is complete and directly continue to execute the following code. Start a Thread by calling the start () method of the Thread class. The Thread is in the ready (runable) State and is not running. Once the cpu time slice is obtained, run () is started () the run () method is called the thread body. It contains the content of the thread to be executed. The Run method ends and the thread ends immediately.
The start method is the method for enabling threads. After using this method, java creates a new thread to execute the methods in run. This is a small demo:
for(int i=0;i<3;i++){ Thread t= new Thread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+" start"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+" end"); } }); t.start(); } System.out.println("it is over");
Execution result:
It is over
Thread-1 start
Thread-0 start
Thread-2 start
Thread-0 end
Thread-1 end
Thread-2 end
Because multithreading is random, the results may be different each time. This is also worth noting that the thread execution sequence is inconsistent with the call sequence.
Run ()
The run method is to call the Runnable run method set by Thread and modify the demo above:
for(int i=0;i<3;i++){ Thread t= new Thread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+" start"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+" end"); } }); t.run(); } System.out.println("it is over");
Execution result:
Main start
Main end
Main start
Main end
Main start
Main end
It is over
The direct result of the run method is very different from that of the start method. It is executed in order and the new thread is not enabled.
Stop ()
The stop method is used to forcibly stop the thread execution. It is not safe. Do not use this method. When you call stop, the locked resources are released. However, such release is inconsistent and may cause program problems. If you want to control the thread stop, you can use custom variables to determine or the isInterrupted () method:
Class Thread1 extends Thread {@ Override public void run () {// determine whether the Thread body is running while (! IsInterrupted () {// Do Something }}}
Interrupt ()
Interrupt is used to notify the thread that you have been interrupted, but the specific interrupted execution needs to be customized in the thread, and you can even ignore the execution. Specifically, when the join, wait, and sleep methods are executed by threads, InterruptedException is thrown.
Thread t1 = new Thread (new Runnable () {@ Override public void run () {System. out. println (Thread. currentThread (). getName () + "start"); try {for (int I = 0; I <100000; I ++) {System. out. println (I + ""); Thread. sleep (1) ;}} catch (InterruptedException e) {System. out. println ("the thread is interrupted"); // you can release resources and log records here. printStackTrace ();} System. out. println (Thread. currentThread (). getName () + "end") ;}}); t1.start (); Thread. sleep (1, 100); t1.interrupt ();
Execution result:
65666768the thread is interruptedjava.lang.InterruptedException: sleep interruptedThread-0 end at java.lang.Thread.sleep(Native Method) at com.wk.aqi.act.Test$1.run(Test.java:23) at java.lang.Thread.run(Thread.java:745)
IsInterrupted ()
Determine whether the thread is interrupted. After the above interrupt method is executed, return true.
SetPriority (int newPriority) and getPriority ()
Sets the thread priority and obtains the thread priority. resources allocated by the cpu are allocated to threads with a higher priority.
Thread t1 = new Thread(new Runnable() { @Override public void run() { long t = System.currentTimeMillis(); System.out.println(Thread.currentThread().getName()+" start"); for(int i=0;i<1000;i++){ try { Thread.sleep(1); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println(Thread.currentThread().getName()+" t1 end "+(System.currentTimeMillis()-t)); } }); Thread t2 = new Thread(new Runnable() { @Override public void run() { long t = System.currentTimeMillis(); System.out.println(Thread.currentThread().getName()+" start"); for(int i=0;i<1000;i++){ try { Thread.sleep(1); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println(Thread.currentThread().getName()+" t2 end "+(System.currentTimeMillis()-t)); } }); t1.setPriority(10); t2.setPriority(1); t2.start(); t1.start();
Execution result:
Thread-0 startThread-1 startThread-0 t1 end 1357Thread-1 t2 end 1371
In the case of the same priority, t1 and t2 are almost completed at the same time. In the case of different priorities, there are significant differences.
GetName ()
It is relatively simple to get the thread name.
Join () and join (long millis)
The jion method is used to wait for the thread to complete execution. The join (long millis) method can set the maximum wait time. For example, the main thread needs to wait for the sub-thread to complete and obtain the results of the sub-thread before it can continue to run. In this case, the join method can be used.
Thread t1 = new Thread (new Runnable () {@ Override public void run () {long t = System. currentTimeMillis (); System. out. println (Thread. currentThread (). getName () + "start"); try {Thread. sleep (1000);} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace ();} System. out. println (Thread. currentThread (). getName () + "t1 end" + (System. currentTimeMillis ()-t) ;}}); t1.start (); t1.join (); System. out. println ("wait for t1 to finish executing, then execute ");
Execution result:
Thread-0 startThread-0 t1 end 1001 wait for t1 to finish executing and then execute
Summary
The above is all the details about the java multi-threaded Thread implementation method code in this article, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message.