Timer 当一个类在另外两个类中自动连接时,obj是如何共享的
我有一节计时器课Timer 当一个类在另外两个类中自动连接时,obj是如何共享的,timer,singleton,autowired,scheduledexecutorservice,Timer,Singleton,Autowired,Scheduledexecutorservice,我有一节计时器课 @Component public class MyTimer { private ScheduledExecutorService schService; public void startTimer() { Instant i=Instant.now(); long l1=i.plusSeconds(30).toEpochMilli(); schService=Executors.newSingle
@Component
public class MyTimer {
private ScheduledExecutorService schService;
public void startTimer() {
Instant i=Instant.now();
long l1=i.plusSeconds(30).toEpochMilli();
schService=Executors.newSingleThreadScheduledExecutor();
Runnable primaryTimeOut = () -> {
if(Instant.now().toEpochMilli()>l1) {
schService.shutdown();
invokeTimeOut();
}
};
schService.scheduleAtFixedRate(primaryTimeOut, 0, 100, TimeUnit.MILLISECONDS);
}
public void stopTimer() {
schService.shutdown();
}
public void invokeTimeOut() {
System.out.println("Timed Out");
}
}
我已在供应商和消费者中自动连接MyTimer,如下所示
class Supplier{
@Autowired
MyTimer t;
eventSupplier(){
t.startTimer();
}
}
class Consumer{
@Autowired
MyTimer t;
eventConsumer(){
t.stopTimer();
}
}
当我从供应商处提供事件时,计时器启动,当我从消费者计时器中获取事件时,计时器停止。
我不明白当消费者呼叫stopTimer时,它是如何工作的。它将如何知道哪个schService obj的关闭将被调用。
当供应商和消费者收到多个请求时,这将如何工作