测试,线程中断测试
parent
81f757e0bd
commit
1aafbe78b9
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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(); // 唤醒线程
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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();*/
|
||||
}
|
||||
}
|
|
@ -32,49 +32,4 @@ public class VehicleInstance {
|
|||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue