-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from jamebal/share
perf: 文件变动后的消息推送
- Loading branch information
Showing
9 changed files
with
88 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/com/jmal/clouddisk/util/ThrottleExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.jmal.clouddisk.util; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.concurrent.*; | ||
|
||
@Slf4j | ||
public class ThrottleExecutor { | ||
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); | ||
private ScheduledFuture<?> scheduledFuture; | ||
private final long delay; | ||
|
||
public ThrottleExecutor(long delay) { | ||
this.delay = delay; | ||
} | ||
|
||
public void schedule(Runnable command) { | ||
if (scheduledFuture != null && !scheduledFuture.isDone()) { | ||
scheduledFuture.cancel(false); | ||
} | ||
scheduledFuture = scheduler.schedule(command, delay, TimeUnit.MILLISECONDS); | ||
} | ||
|
||
public void shutdown() { | ||
scheduler.shutdown(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
ThrottleExecutor throttleExecutor = new ThrottleExecutor(500); // 节流时间500毫秒 | ||
|
||
for (int i = 0; i < 10; i++) { | ||
final int param = i; | ||
throttleExecutor.schedule(() -> { | ||
System.out.println("Executing task with param: " + param); | ||
}); | ||
try { | ||
Thread.sleep(100); // 模拟频繁调用 | ||
} catch (InterruptedException e) { | ||
log.error("Error occurred: ", e); | ||
} | ||
} | ||
|
||
throttleExecutor.shutdown(); | ||
} | ||
} |