TitanWong преди 1 година
родител
ревизия
171a59f1a4
променени са 1 файла, в които са добавени 37 реда и са изтрити 32 реда
  1. 37 32
      src/main/java/com/zhili/stationcontrol/tts/service/impl/MusicServiceImpl.java

+ 37 - 32
src/main/java/com/zhili/stationcontrol/tts/service/impl/MusicServiceImpl.java

@@ -1,6 +1,7 @@
 package com.zhili.stationcontrol.tts.service.impl;
 
 import com.zhili.stationcontrol.tts.service.MusicService;
+import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.stereotype.Service;
@@ -29,7 +30,7 @@ public class MusicServiceImpl implements MusicService, Runnable {
     Thread thread;
 
     @PostConstruct
-    public void init(){
+    public void init() {
         this.thread = new Thread(this);
         this.thread.start();
     }
@@ -55,47 +56,51 @@ public class MusicServiceImpl implements MusicService, Runnable {
     }
 
 
+    @SneakyThrows
     @Override
     public void run() {
         while (true) {
-            try {
-                Thread.sleep(500);
-                log.info("----------");
-                log.info("startCmd:{},endCmd:{},repeat:{},cmd:{}", startCmd, endCmd, repeat, cmd);
-                log.info("p:{},p.isAlive:{}", p, p == null ? null : p.isAlive());
-                if (startCmd) {
-                    startCmd = false;
-                    if (p != null) {
-                        p.destroyForcibly();
-                        p.waitFor();
-                        p = null;
+            Thread.sleep(500);
+            synchronized (this) {
+                try {
+                    log.info("----------");
+                    log.info("startCmd:{},endCmd:{},repeat:{},cmd:{}", startCmd, endCmd, repeat, cmd);
+                    log.info("p:{},p.isAlive:{}", p, p == null ? null : p.isAlive());
+                    if (startCmd) {
+                        startCmd = false;
+                        if (p != null) {
+                            p.destroyForcibly();
+                            p.waitFor();
+                            p = null;
+                        }
+                        p = Runtime.getRuntime().exec(cmd);
                     }
-                    p = Runtime.getRuntime().exec(cmd);
-                }
-                if (endCmd) {
-                    endCmd = false;
-                    if (p != null) {
-                        p.destroyForcibly();
-                        p.waitFor();
-                        p = null;
+                    if (endCmd) {
+                        endCmd = false;
+                        if (p != null) {
+                            p.destroyForcibly();
+                            p.waitFor();
+                            p = null;
+                        }
                     }
-                }
-                if (!startCmd && !endCmd && repeat && cmd != null) {
-                    if (p != null) {
-                        boolean exit = p.waitFor(500, TimeUnit.MILLISECONDS);
-                        if (exit) {
-                            log.info("repeat");
-                            p = Runtime.getRuntime().exec(cmd);
+                    if (!startCmd && !endCmd && repeat && cmd != null) {
+                        if (p != null) {
+                            boolean exit = p.waitFor(500, TimeUnit.MILLISECONDS);
+                            if (exit) {
+                                log.info("repeat");
+                                p = Runtime.getRuntime().exec(cmd);
+                            } else {
+                                return;
+                            }
                         } else {
-                            return;
+                            p = Runtime.getRuntime().exec(cmd);
                         }
-                    } else {
-                        p = Runtime.getRuntime().exec(cmd);
                     }
+                } catch (Exception e) {
+                    e.printStackTrace();
                 }
-            } catch (Exception e) {
-                e.printStackTrace();
             }
+
         }
     }
 }