说我有这个example肿的例子。 我期望发生的事情是在控制台中获得大约三倍的数字,然后我希望打印“任务已停止”行,然后再没有更多的数字。
所以我想停止执行任务。到目前为止,我已经尝试过cancel(true)
,executor.shutdownNow)(
和executor.shutdown()
的执行者,但是这些都没有导致我上面描述的行为。我想念什么吗?有没有办法取消此任务?
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
executor.setRemoveOnCancelPolicy(true);
final int rareNumber = 302330233;
ScheduledFuture<?> future = executor.scheduleAtFixedRate(() -> {
int x = 0;
while (true) {
x++;
if(x % rareNumber == 0) {
System.out.println(x);
}
}
}, 1, 2, TimeUnit.SECONDS);
Thread.sleep(3_000);
// I'd like to stop a task here.
System.out.println("The task is stopped");
答案 0 :(得分:1)
正确的方法是+----------+-----+------+------+------+
| Date | Loc | Hour | Rain | Wind |
+----------+-----+------+------+------+
| 1-Sep-19 | 1 | 1 | 184 | 65 |
| 1-Sep-19 | 1 | 2 | 0 | 0 |
| 1-Sep-19 | 1 | 3 | 126 | 64 |
| 2-Sep-19 | 1 | 1 | 112 | 63 |
| 2-Sep-19 | 1 | 2 | 155 | 0 |
| 2-Sep-19 | 1 | 3 | 186 | 50 |
| 3-Sep-19 | 1 | 1 | 0 | 0 |
| 3-Sep-19 | 1 | 2 | 154 | 79 |
| 3-Sep-19 | 1 | 3 | 143 | 61 |
| 1-Sep-19 | 2 | 1 | 187 | 73 |
| 1-Sep-19 | 2 | 2 | 173 | 63 |
| 1-Sep-19 | 2 | 3 | 186 | 63 |
| 2-Sep-19 | 2 | 1 | 0 | 0 |
| 2-Sep-19 | 2 | 2 | 0 | 0 |
| 2-Sep-19 | 2 | 3 | 0 | 0 |
| 3-Sep-19 | 2 | 1 | 172 | 56 |
| 3-Sep-19 | 2 | 2 | 156 | 56 |
| 3-Sep-19 | 2 | 3 | 176 | 79 |
+----------+-----+------+------+------+
。这将导致线程被中断。
但是那时您的线程没有处于等待/休眠状态,因此您没有得到 import pandas as pd
import numpy as np
df = pd.read_csv('data.csv')
d1 = date(2019, 9, 1)
d2 = date(2019, 9, 3)
delta = d2 - d1
# below indexes were created to make the code more dynamic & scalable easily
idx_date = [(d1 + timedelta(days=i)) for i in range(delta.days + 1)]
idx_loc = np.arange(1,3)
idx_hour = np.arange(1,4)
cols =['Rain', 'Wind']
df_filled = df.reindex(index=[idx_date,idx_loc,idx_hour], columns=cols, fill_value=0)
,因此需要检查'Access-Control-Allow-Origin' '*';
标志:
executor.shutdownNow()
答案 1 :(得分:1)
首先,当您在一次执行中找到正确的数字时,应该中断while
循环以完成该执行周期。
ScheduledFuture<?> future = executor.scheduleAtFixedRate(() -> {
int x = 0;
while (true) {
x++;
if(x % rareNumber == 0) {
System.out.println(x);
break;
}
}
}, 1, 2, TimeUnit.SECONDS);
然后,您可以按如下所示取消未来,以停止固定汇率的计划程序。
Thread.sleep(3_000);
// I'd like to stop a task here.
future.cancel(true);
System.out.println("The task is stopped");
答案 2 :(得分:1)
您应该在代码中考虑几件事:
ExecutorService
文档中描述了如何执行此操作:void shutdownAndAwaitTermination(ExecutorService pool) {
pool.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
pool.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!pool.awaitTermination(60, TimeUnit.SECONDS))
System.err.println("Pool did not terminate");
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
pool.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
如果您想使任务可取消,则有两种常用方法:
Thread.currentThread().isInterrupted()
状态InterruptedException
,则可以处理此异常。线程中断时将引发该事件。按照您设置的executor.setRemoveOnCancelPolicy(true);
,如果线程被中断,则不会重新创建线程。如果您未设置此标志-最好保留中断状态,因为您不是这些线程的所有者,并且您不知道中断对它们意味着什么。但是您没有使用任何可以抛出InterruptedException
的方法,所以没关系。