测试,线程中断测试

new-master
DongZeLiang 2023-11-17 09:37:34 +08:00
parent 81f757e0bd
commit 1aafbe78b9
4 changed files with 119 additions and 45 deletions

View File

@ -0,0 +1,25 @@
package com.muyu.common;
import com.muyu.vehicle.MyThread;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* @author DongZl
* @description: 线
* @Date 2023-11-17 09:16
*/
public class ThreadPool {
/**
* 线
*/
private static final ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(1);
public static void submit (Runnable thread){
// 参数分别是: 任务, 多久后开始执行, 每隔多久执行一次(周期),时间单位
scheduledThreadPool.scheduleAtFixedRate(thread, 0,1, TimeUnit.SECONDS);
}
}

View File

@ -0,0 +1,37 @@
package com.muyu.vehicle;
public class MyThread implements Runnable {
/**
*
*/
private volatile boolean isPaused;
@Override
public void run() {
if (!isPaused){
System.out.println(System.currentTimeMillis());
}else {
System.out.println("线程暂停");
}
}
/**
* 线
*/
public void pause() {
isPaused = true;
}
/**
* 线
*/
public void resume() {
isPaused = false;
synchronized (this) {
notify(); // 唤醒线程
}
}
}

View File

@ -0,0 +1,57 @@
package com.muyu.vehicle;
import com.muyu.common.ThreadPool;
import static java.lang.Thread.sleep;
/**
* @author DongZl
* @description:
* @Date 2023-11-17 09:02
*/
public class Test {
public static void main(String[] args) throws InterruptedException {
MyThread thread = new MyThread();
ThreadPool.submit(thread);
new Thread(() -> {
try {
sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
thread.pause();
}).start();
/*new Thread(() -> {
try {
sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("执行了五秒,等待五秒,在开始");
try {
synchronized (thread){
thread.wait();
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("等待了五秒现在开始3秒后杀死");
synchronized (thread){
thread.notify();
}
try {
sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
synchronized (thread){
thread.interrupt();
}
System.out.println("停止成功" + thread.isAlive());
}).start();*/
}
}

View File

@ -32,49 +32,4 @@ public class VehicleInstance {
private VehicleData vehicleData; private VehicleData vehicleData;
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
while (true){
try {
sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(System.currentTimeMillis());
}
});
thread.start();
new Thread(() -> {
try {
sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("执行了五秒,等待五秒,在开始");
try {
synchronized (thread){
thread.wait();
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("等待了五秒现在开始3秒后杀死");
synchronized (thread){
thread.notify();
}
try {
sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
synchronized (thread){
thread.interrupt();
}
System.out.println("停止成功" + thread.isAlive());
}).start();
}
} }