mirror of
https://github.com/Coloryr/AllMusic.git
synced 2026-11-21 20:52:31 +00:00
up 线程管理
This commit is contained in:
1 parent
adbc160397
commit
d311b81805
6 files changed
+58
-92
No files matched your search
+7
-4
@@ -47,7 +47,7 @@ public class AllMusic {
|
||||
/**
|
||||
* 插件版本号
|
||||
*/
|
||||
public static final String version = "3.8.0";
|
||||
public static final String version = "3.9.0";
|
||||
/**
|
||||
* 配置文件版本号
|
||||
*/
|
||||
@@ -338,6 +338,8 @@ public class AllMusic {
|
||||
* 启动插件
|
||||
*/
|
||||
public static void start() {
|
||||
isRun = true;
|
||||
|
||||
MusicHttpClient.init();
|
||||
|
||||
IMusicApi api = new NetiApiMain();
|
||||
@@ -355,13 +357,14 @@ public class AllMusic {
|
||||
* 停止插件
|
||||
*/
|
||||
public static void stop() {
|
||||
isRun = false;
|
||||
|
||||
PlayMusic.clearVote();
|
||||
PlayMusic.clearPush();
|
||||
side.sendStop();
|
||||
MusicSearch.stop();
|
||||
PlayMusic.stop();
|
||||
PlayMusic.clear();
|
||||
PlayRuntime.stop();
|
||||
DataSql.stop();
|
||||
side.sendStop();
|
||||
log.data("<light_purple>[AllMusic3]<dark_green><yellow>已停止,感谢使用");
|
||||
}
|
||||
|
||||
|
||||
@@ -12,10 +12,10 @@ import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
public class MusicSearch {
|
||||
private static final Queue<PlayerAddMusicObj> tasks = new ConcurrentLinkedQueue<>();
|
||||
private static boolean isRun;
|
||||
|
||||
private static void task() {
|
||||
while (isRun) {
|
||||
AllMusic.log.data("歌曲搜索线程启动");
|
||||
while (AllMusic.isRun) {
|
||||
try {
|
||||
PlayerAddMusicObj obj = tasks.poll();
|
||||
if (obj != null) {
|
||||
@@ -40,16 +40,11 @@ public class MusicSearch {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
AllMusic.log.data("歌曲搜索线程停止");
|
||||
}
|
||||
|
||||
public static void start() {
|
||||
Thread taskT = new Thread(MusicSearch::task, "AllMusic_search");
|
||||
isRun = true;
|
||||
taskT.start();
|
||||
}
|
||||
|
||||
public static void stop() {
|
||||
isRun = false;
|
||||
new Thread(MusicSearch::task, "allmusic_search").start();
|
||||
}
|
||||
|
||||
public static void addSearch(PlayerAddMusicObj obj) {
|
||||
|
||||
@@ -77,24 +77,13 @@ public class PlayMusic {
|
||||
* 插歌目标
|
||||
*/
|
||||
private static SongInfoObj push;
|
||||
private static boolean isRun;
|
||||
private static int idleNow;
|
||||
|
||||
/**
|
||||
* 停止歌曲逻辑
|
||||
*/
|
||||
public static void stop() {
|
||||
PlayMusic.clear();
|
||||
isRun = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始歌曲逻辑
|
||||
*/
|
||||
public static void start() {
|
||||
Thread addT = new Thread(PlayMusic::task, "AllMusicList");
|
||||
isRun = true;
|
||||
addT.start();
|
||||
new Thread(PlayMusic::task, "allmusic_task").start();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -218,7 +207,8 @@ public class PlayMusic {
|
||||
}
|
||||
|
||||
private static void task() {
|
||||
while (isRun) {
|
||||
AllMusic.log.data("歌曲处理线程启动");
|
||||
while (AllMusic.isRun) {
|
||||
try {
|
||||
PlayerAddMusicObj obj = tasks.poll();
|
||||
if (obj != null) {
|
||||
@@ -233,6 +223,7 @@ public class PlayMusic {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
AllMusic.log.data("歌曲处理线程关闭");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+36
-57
@@ -18,7 +18,6 @@ public class PlayRuntime {
|
||||
*/
|
||||
private static int count = 0;
|
||||
private static int ping = 0;
|
||||
private static boolean isRun;
|
||||
/**
|
||||
* 歌曲更新计数器
|
||||
*/
|
||||
@@ -27,23 +26,21 @@ public class PlayRuntime {
|
||||
* 歌曲定时器
|
||||
*/
|
||||
private static ScheduledExecutorService service;
|
||||
/**
|
||||
* 歌词定时器
|
||||
*/
|
||||
private static ScheduledExecutorService service1;
|
||||
/**
|
||||
* 事务定时器
|
||||
*/
|
||||
private static ScheduledExecutorService service2;
|
||||
|
||||
private static boolean isPlay;
|
||||
|
||||
/**
|
||||
* 启动歌曲工作
|
||||
*/
|
||||
public static void start() {
|
||||
Thread taskT = new Thread(PlayRuntime::musicPlayTask, "AllMusicPlay");
|
||||
isRun = true;
|
||||
taskT.start();
|
||||
new Thread(PlayRuntime::musicPlayTask, "allMusic_play").start();
|
||||
|
||||
service = Executors.newSingleThreadScheduledExecutor();
|
||||
service.scheduleAtFixedRate(PlayRuntime::time1, 0, 10, TimeUnit.MILLISECONDS);
|
||||
service2 = Executors.newSingleThreadScheduledExecutor();
|
||||
service2.scheduleAtFixedRate(PlayRuntime::time3, 0, 1, TimeUnit.SECONDS);
|
||||
}
|
||||
@@ -52,40 +49,22 @@ public class PlayRuntime {
|
||||
* 停止歌曲工作
|
||||
*/
|
||||
public static void stop() {
|
||||
closeTimer();
|
||||
if (service2 != null) {
|
||||
service2.shutdown();
|
||||
service2 = null;
|
||||
}
|
||||
isRun = false;
|
||||
PlayMusic.musicLessTime = 0;
|
||||
}
|
||||
|
||||
private static void closeTimer() {
|
||||
if (service != null) {
|
||||
service.shutdown();
|
||||
service = null;
|
||||
}
|
||||
if (service1 != null) {
|
||||
service1.shutdown();
|
||||
service1 = null;
|
||||
}
|
||||
}
|
||||
|
||||
private static void startTimer() {
|
||||
service = Executors.newSingleThreadScheduledExecutor();
|
||||
service.scheduleAtFixedRate(PlayRuntime::time1, 0, 10, TimeUnit.MILLISECONDS);
|
||||
if (PlayMusic.lyric != null && PlayMusic.lyric.isHaveLyric()) {
|
||||
service1 = Executors.newSingleThreadScheduledExecutor();
|
||||
service1.scheduleAtFixedRate(PlayRuntime::time2, 0, 2, TimeUnit.MILLISECONDS);
|
||||
if (service2 != null) {
|
||||
service2.shutdown();
|
||||
service2 = null;
|
||||
}
|
||||
PlayMusic.musicLessTime = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空歌曲数据
|
||||
*/
|
||||
private static void clear() {
|
||||
closeTimer();
|
||||
isPlay = false;
|
||||
PlayMusic.musicNowTime = 0;
|
||||
PlayMusic.musicAllTime = 0;
|
||||
PlayMusic.musicLessTime = 0;
|
||||
@@ -102,37 +81,35 @@ public class PlayRuntime {
|
||||
* 歌曲时间定时器
|
||||
*/
|
||||
private static void time1() {
|
||||
PlayMusic.musicNowTime += 10;
|
||||
count++;
|
||||
if (count == 100) {
|
||||
PlayMusic.musicLessTime--;
|
||||
count = 0;
|
||||
if (isPlay) {
|
||||
PlayMusic.musicNowTime += 10;
|
||||
count++;
|
||||
if (count == 100) {
|
||||
PlayMusic.musicLessTime--;
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 歌词更新
|
||||
*/
|
||||
private static void time2() {
|
||||
try {
|
||||
if (PlayMusic.lyric == null)
|
||||
return;
|
||||
boolean res = PlayMusic.lyric
|
||||
.checkTime(PlayMusic.musicNowTime, AllMusic.getConfig().ktvMode);
|
||||
if (res) {
|
||||
times = 0;
|
||||
HudUtils.sendHudLyricData();
|
||||
AllMusic.side.updateLyric();
|
||||
} else {
|
||||
times++;
|
||||
if (times == AllMusic.getConfig().sendDelay / 2 && PlayMusic.lyric != null) {
|
||||
if (PlayMusic.lyric != null && PlayMusic.lyric.isHaveLyric()) {
|
||||
try {
|
||||
if (PlayMusic.lyric == null)
|
||||
return;
|
||||
boolean res = PlayMusic.lyric.checkTime(PlayMusic.musicNowTime, AllMusic.getConfig().ktvMode);
|
||||
if (res) {
|
||||
times = 0;
|
||||
HudUtils.sendHudLyricData();
|
||||
AllMusic.side.updateLyric();
|
||||
} else {
|
||||
times++;
|
||||
if (times == AllMusic.getConfig().sendDelay / 2 && PlayMusic.lyric != null) {
|
||||
times = 0;
|
||||
HudUtils.sendHudLyricData();
|
||||
AllMusic.side.updateLyric();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -213,7 +190,8 @@ public class PlayRuntime {
|
||||
}
|
||||
|
||||
private static void musicPlayTask() {
|
||||
while (isRun) {
|
||||
AllMusic.log.data("歌曲播放线程启动");
|
||||
while (AllMusic.isRun) {
|
||||
try {
|
||||
if (PlayMusic.getListSize() == 0) {
|
||||
if (PlayMusic.error >= 10) {
|
||||
@@ -258,7 +236,7 @@ public class PlayRuntime {
|
||||
|
||||
if (PlayMusic.nowPlayMusic.getLength() != 0) {
|
||||
PlayMusic.musicAllTime = PlayMusic.musicLessTime = (PlayMusic.nowPlayMusic.getLength() / 1000) + 3;
|
||||
startTimer();
|
||||
isPlay = true;
|
||||
AllMusic.side.sendMusic(PlayMusic.url);
|
||||
if (!AllMusic.getConfig().mutePlayMessage) {
|
||||
SongInfoObj music = PlayMusic.nowPlayMusic;
|
||||
@@ -311,5 +289,6 @@ public class PlayRuntime {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
AllMusic.log.data("歌曲播放线程停止");
|
||||
}
|
||||
}
|
||||
@@ -81,7 +81,6 @@ public class DataSql {
|
||||
* 数据库文件
|
||||
*/
|
||||
public static File sqlFile;
|
||||
private static boolean isRun;
|
||||
/**
|
||||
* 数据库链接对象
|
||||
*/
|
||||
@@ -731,26 +730,24 @@ public class DataSql {
|
||||
*/
|
||||
public static void start() {
|
||||
init();
|
||||
Thread thread = new Thread(DataSql::run);
|
||||
isRun = true;
|
||||
thread.start();
|
||||
new Thread(DataSql::run).start();
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止数据库
|
||||
*/
|
||||
public static void stop() {
|
||||
isRun = false;
|
||||
semaphore.release();
|
||||
}
|
||||
|
||||
private static void run() {
|
||||
AllMusic.log.data("数据库线程启动");
|
||||
Runnable runnable;
|
||||
while (isRun) {
|
||||
while (AllMusic.isRun) {
|
||||
try {
|
||||
semaphore.acquire();
|
||||
if (!isRun)
|
||||
return;
|
||||
if (!AllMusic.isRun)
|
||||
break;
|
||||
do {
|
||||
runnable = tasks.poll();
|
||||
if (runnable != null) {
|
||||
@@ -762,5 +759,6 @@ public class DataSql {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
AllMusic.log.data("数据库线程关闭");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user