diff --git a/src/main/java/com/muyu/common/ThreadPool.java b/src/main/java/com/muyu/common/ThreadPool.java new file mode 100644 index 0000000..ef15ed1 --- /dev/null +++ b/src/main/java/com/muyu/common/ThreadPool.java @@ -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); + } +} diff --git a/src/main/java/com/muyu/vehicle/MyThread.java b/src/main/java/com/muyu/vehicle/MyThread.java new file mode 100644 index 0000000..9a5c6b0 --- /dev/null +++ b/src/main/java/com/muyu/vehicle/MyThread.java @@ -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(); // 唤醒线程 + } + } + + +} diff --git a/src/main/java/com/muyu/vehicle/Test.java b/src/main/java/com/muyu/vehicle/Test.java new file mode 100644 index 0000000..fcd2a94 --- /dev/null +++ b/src/main/java/com/muyu/vehicle/Test.java @@ -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();*/ + } +} diff --git a/src/main/java/com/muyu/vehicle/model/VehicleInstance.java b/src/main/java/com/muyu/vehicle/model/VehicleInstance.java index 2f92f29..14f930b 100644 --- a/src/main/java/com/muyu/vehicle/model/VehicleInstance.java +++ b/src/main/java/com/muyu/vehicle/model/VehicleInstance.java @@ -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(); - } - }