I have briefly introduced the use of Java multi-threading. I have already introduced the Thread class and Runnable class. In order to better understand multi-threading, this article conducts a detailed analysis of Thread.
start()
Let’s first take a look at the introduction of this method in the API:
Makes the thread start executing; the Java virtual machine calls the thread's run method.
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 illegal to start a thread multiple times. Especially when the thread has finished executing, it cannot be restarted.
Use the start method to start the thread, which truly realizes multi-threading. At this time, you do not need to wait for the run method body code to complete execution and continue to execute the following code directly. A thread is started by calling the start() method of the Thread class. At this time, the thread is in a ready (runable) state and is not running. Once the CPU time slice is obtained, the run() method begins to be executed. Here, the method run() is called the thread body. It contains the content of the thread to be executed. The Run method ends and the thread terminates immediately.
The start method is a method to start the thread. After using it, Java will create a new thread to execute the method in run. Here 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 results:
it is over
Thread-1 start
Thread-0 start
Thread-2 start
Thread-0 end
Thread-1 end
Thread-2 end
Since multithreading is random, the results may be different each time, which is also something we need to pay attention to. The execution order and call order of threads are not consistent.
run()
The run method is to call the run method of Runnable set by Thread and modify the above 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.run(); } System.out.println("it is over"); Execution results:
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 the start. It is executed in order and does not open a new thread.
stop()
The stop method is to force stop the execution of the thread. It is not safe, so do not use this method. When stop is called, the locked resources will be released, but this release is inconsistent and can easily cause program problems. If you want to control the stop of the thread, you can use custom variables to judge or isInterrupted() method:
class Thread1 extends Thread { @Override public void run() { //Judge whether the thread body is running while (!isInterrupted()) { // Do Something } } } interrupt()
The function of interrupt is to notify the thread that you have been interrupted, but the specific interrupt execution requires custom processing of the thread, and you can even ignore it and continue execution. The specific loneliness is to throw an InterruptedException when threading execution of join, wait, and sleep methods.
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 do resource release, logging, etc. e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+" end"); } }); t1.start(); Thread.sleep(100); t1.interrupt();Execution results:
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()
To determine whether the thread is interrupted, after executing the above interrupt method, it will return true.
setPriority(int newPriority) and getPriority()
Set the priority of threads and get the priority of threads. The resources allocated by CPU are focused on threads with higher priorities.
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 results:
Thread-0 startThread-1 startThread-0 t1 end 1357Thread-1 t2 end 1371
When the priority is the same, t1 and t2 are completed almost at the same time, and there are obvious differences when the priority is different.
getName()
It's relatively simple, get the name of the thread.
join() and join(long millis)
The function of the jion method is to wait for the thread execution to complete, and join (long millis) can set the maximum waiting time. For example, the main thread needs to wait for the child thread to complete and obtain the result of the child thread before continuing to execute. At this time, you can use the join method.
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("Waiting for t1 to be executed before executing");Execution results:
Thread-0 startThread-0 t1 end 1001 Wait for t1 to be executed before executing
Summarize
The above is all the detailed explanation of the implementation method code of Java multi-threaded Thread, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out.