Multithread Learning Notes Chapter 1 (2)
- Stop thread
- Pause thread
- yield() method
- thread priority
- Daemon thread
Stop thread
Use the interrupt() method (threads that cannot stop)
Let's first look at two ways to determine whether a thread has stopped, interrupted() and isInterrupted().
/**
* Test the difference between Interrupted and Isinterrupted methods
*/
public class Code_16_InterruptedAndIsinterruptedTest {
private static class MyThread extends Thread {
@Override
public void run() {
super.run();
for (int i = 0; i < 5000; i++) {
System.out.println("i = " + (i + 1));
}
}
}
public static void main(String[] args) {
System.out.println("-----------test1---------");
test1();
System.out.println("-----------test2---------");
test2();
System.out.println("-----------test3---------");
test3();
}
public static void test1(){
try {
MyThread thread = new MyThread();
thread.start();
Thread.sleep(1000);
thread.interrupt();
//Thread.currentThread().interrupt();
System.out.println("Whether to stop 1 ="+thread.interrupted()); //false
System.out.println("Whether to stop 2 ="+thread.interrupted());//false
} catch (InterruptedException e) {
System.out.println("main catch");
e.printStackTrace();
}
System.out.println("end!");
}
/**
* The current thread is that the main thread can stop, but the second layer is false because interrupted clears.
*/
public static void test2(){
Thread.currentThread().interrupt();//Stop the current main thread
System.out.println("Whether to stop 1 =" + Thread.interrupted());//true
System.out.println("Whether to stop 2 =" + Thread.interrupted());//false
System.out.println("end!");
}
/**
* isInterrupted Use of methods
*/
public static void test3(){
try {
MyThread thread = new MyThread();
thread.start();
Thread.sleep(1000);
thread.interrupt();
System.out.println("Whether to stop 1 ="+thread.isInterrupted());//false
System.out.println("Whether to stop 2 ="+thread.isInterrupted());//false
} catch (InterruptedException e) {
System.out.println("main catch");
e.printStackTrace();
}
System.out.println("end!");
}
}
The output of the first two false s is because the current thread is the main thread, which does not stop. The second one stops the main thread, but the second one is because the interrupted method has the function of clearing the state. The third is Interrupted does not have this functionality.
Stop Thread Method:
- You can stop threads using the exception method
- Stop in Sleep (the sleep method is inside the fun method)
- stop violence (not recommended)
- Use Return and Interrupt to stop threads
package ThreadCoreTechnology.chapter1;
/**
* Stop threads with exception method
*/
public class Code_17_StopForCatchTest {
private static class MyThread extends Thread {
@Override
public void run() {
super.run();
try {
for (int i = 0; i < 500000; i++) {
if (this.interrupted()) {
System.out.println("It's stopped. It's going to quit.!");
throw new InterruptedException();
}
System.out.println("i=" + (i + 1));
}
System.out.println("I am here for Below! ");
} catch (InterruptedException e) {
System.out.println("enter MyThread.java Class run Method catch Yes!");
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
MyThread thread = new MyThread();
thread.start();
Thread.sleep(3000);
thread.interrupt();
} catch (InterruptedException e) {
System.out.println("main catch");
e.printStackTrace();
}
System.out.println("end!");
}
}
/**
* Use Return and Interrupt to End Threads
*/
public class Code_18_ReturnInterruptedTest {
private static class MyThread extends Thread {
@Override
public void run() {
while (true) {
if (this.isInterrupted()) {
System.out.println("Stopped.!");
return; //End
}
System.out.println("timer = " + System.currentTimeMillis());
}
}
}
public static void main(String[] args) throws InterruptedException {
MyThread t=new MyThread();
t.start();
Thread.sleep(2000);
t.interrupt(); //End with interrupt and return
}
}
Pause thread
Suspend the thread means recovery. suspend the thread and resume() method is used to restore the execution of the thread.
suspend() method is also easy to cause asynchrony, so use it very carefully.
yield() method
Look at the following code:
/**
* Abandoning the current CPU resources to other tasks
*/
public class Code_19_YieldTest {
private static class MyThread extends Thread {
@Override
public void run() {
long beginTime = System.currentTimeMillis();
int count = 0;
for (int i = 0; i < 50000000; i++) {
// Thread.yield();
count = count + (i + 1);
}
long endTime = System.currentTimeMillis();
System.out.println("Use time: " + (endTime - beginTime) + "Millisecond!");
}
}
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
Output without yield():
Time: 26 milliseconds!
Add yield() method output:
Time: 20217 milliseconds!
thread priority
It has three characteristics:
- Inheritance
- Regularity
- Randomness
(1) Thread priority has inheritance:
/**
* Thread priority has inheritance
*/
public class Code_20_PriorityTest {
private static class MyThread1 extends Thread {
@Override
public void run() {
System.out.println("MyThread1 run priority=" + this.getPriority());
MyThread2 thread2 = new MyThread2();
thread2.start();
}
}
private static class MyThread2 extends Thread {
@Override
public void run() {
System.out.println("MyThread2 run priority=" + this.getPriority());
}
}
public static void main(String[] args) {
System.out.println("main thread begin priority="
+ Thread.currentThread().getPriority());
Thread.currentThread().setPriority(6);
System.out.println("main thread end priority="
+ Thread.currentThread().getPriority());
MyThread1 thread1 = new MyThread1();
thread1.start();
}
}
Effect
(2) Thread priority is regular:
- High priority threads always execute most of the code first, but it does not mean that all high priority threads execute first.
- Priority has nothing to do with the execution order of code (when the priority gap is large), which indicates that priority has certain regularity, that is, CPU tries to give resources to threads with higher priority.
(3) Thread priority is random
Daemon thread
Look at the following code:
/**
* Daemon thread
*/
public class Code_21_DaemonTest {
private static class MyThread extends Thread {
private int i = 0;
@Override
public void run() {
try {
while (true) { //It had been printing all the time.
i++;
System.out.println("i = " + (i));
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
MyThread thread = new MyThread();
thread.setDaemon(true); //Set the daemon thread // If this sentence is commented out, it will be printed all the time.
thread.start();
Thread.sleep(5000);
System.out.println("I leave thread Objects are no longer printed, that is, they stop.!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
The thread.setDaemon(true) in the code; if you comment out this sentence, it will be printed all the time, because after setting this, the thread sleeps and the daemon thread is gone, and it ends.