如何让ScheduleTask在某个时间执行一次任务?
How to make ScheduleTask perform a task once at a certain time?
最近在使用 ScheduleTask,我有这样一个问题。当地图大小超过 MAX_SIZE
字段时,我希望我的 ScheduleTask 每 5 秒从我的地图中删除元素。我正在尝试这样做:
public class RemoverThread extends TimerTask {
private AbstractCustomCache customCache;
private static final int MAX_SIZE = 2;
public RemoverThread(AbstractCustomCache customCache) {
this.customCache = customCache;
}
@Override
public void run() {
if (customCache.getCacheEntry().size() > MAX_SIZE) {
List<String> gg = new ArrayList<>(customCache.getCacheEntry().keySet());
for (String s : gg) {
System.out.println("Element was remove: " + customCache.getCacheEntry().remove(s));
System.out.println("Time: " + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(Calendar.getInstance().getTime()));
if (customCache.getCacheEntry().size() <= MAX_SIZE) {
break;
}
}
}
}
}
我的主要:
public class Main {
public static void main(String[] args) {
AbstractCustomCacheStatistics gh = new MapCacheStatistics();
AbstractCustomCache gg = new MapCache(gh);
for (int i = 0; i < 5; i++){
gg.put("ab" + i);
}
RemoverThread removerThread = new RemoverThread(gg);
Executors.newScheduledThreadPool(2).scheduleAtFixedRate(removerThread, 5, 5, TimeUnit.SECONDS);
for (int i = 0; i < 3; i++){
gg.put("abc" + i);
}
}
}
抽象自定义缓存:
public abstract class AbstractCustomCache implements CustomCache{
private Map<String, CacheEntry> cacheEntry = new LinkedHashMap<>();
public Map<String, CacheEntry> getCacheEntry() {
return Collections.synchronizedMap(cacheEntry);
}
}
我的输出中有这个:
Element was remove: CacheEntry{field='ab0'}
Time: 2019/01/31 11:39:11
Element was remove: CacheEntry{field='ab1'}
Time: 2019/01/31 11:39:11
Element was remove: CacheEntry{field='ab2'}
Time: 2019/01/31 11:39:11
Element was remove: CacheEntry{field='ab3'}
Time: 2019/01/31 11:39:11
Element was remove: CacheEntry{field='ab4'}
Time: 2019/01/31 11:39:11
Element was remove: CacheEntry{field='abc0'}
Time: 2019/01/31 11:39:11
我做错了什么?如何改进?我想每 5 秒从地图中删除一次。比如我加了ab0, ab1, ab2, ab3, ab4
。流应该删除 ab0, ab1, ab2
。由于地图中的元素大于MAX_SIZE
。然后我添加 abc0, abc1, abc2
。在删除第一个元素 5 秒后,应该删除 ab3, ab4, abc0
。但是,如您所见,所有项目都被同时删除。
What am I doing wrong?
和Executors.newScheduledThreadPool(2).scheduleAtFixedRate(removerThread, 5, 5, TimeUnit.SECONDS);
,第一次执行时间会delayed秒5
秒,此时缓存包含:ab0 ab1 ab2 ab3 ab4 abc0 abc1 ab2
,前 5 个将被删除。
你需要一个线程安全的版本AbstractCustomCache customCache
因为它被多个线程修改。
最近在使用 ScheduleTask,我有这样一个问题。当地图大小超过 MAX_SIZE
字段时,我希望我的 ScheduleTask 每 5 秒从我的地图中删除元素。我正在尝试这样做:
public class RemoverThread extends TimerTask {
private AbstractCustomCache customCache;
private static final int MAX_SIZE = 2;
public RemoverThread(AbstractCustomCache customCache) {
this.customCache = customCache;
}
@Override
public void run() {
if (customCache.getCacheEntry().size() > MAX_SIZE) {
List<String> gg = new ArrayList<>(customCache.getCacheEntry().keySet());
for (String s : gg) {
System.out.println("Element was remove: " + customCache.getCacheEntry().remove(s));
System.out.println("Time: " + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(Calendar.getInstance().getTime()));
if (customCache.getCacheEntry().size() <= MAX_SIZE) {
break;
}
}
}
}
}
我的主要:
public class Main {
public static void main(String[] args) {
AbstractCustomCacheStatistics gh = new MapCacheStatistics();
AbstractCustomCache gg = new MapCache(gh);
for (int i = 0; i < 5; i++){
gg.put("ab" + i);
}
RemoverThread removerThread = new RemoverThread(gg);
Executors.newScheduledThreadPool(2).scheduleAtFixedRate(removerThread, 5, 5, TimeUnit.SECONDS);
for (int i = 0; i < 3; i++){
gg.put("abc" + i);
}
}
}
抽象自定义缓存:
public abstract class AbstractCustomCache implements CustomCache{
private Map<String, CacheEntry> cacheEntry = new LinkedHashMap<>();
public Map<String, CacheEntry> getCacheEntry() {
return Collections.synchronizedMap(cacheEntry);
}
}
我的输出中有这个:
Element was remove: CacheEntry{field='ab0'}
Time: 2019/01/31 11:39:11
Element was remove: CacheEntry{field='ab1'}
Time: 2019/01/31 11:39:11
Element was remove: CacheEntry{field='ab2'}
Time: 2019/01/31 11:39:11
Element was remove: CacheEntry{field='ab3'}
Time: 2019/01/31 11:39:11
Element was remove: CacheEntry{field='ab4'}
Time: 2019/01/31 11:39:11
Element was remove: CacheEntry{field='abc0'}
Time: 2019/01/31 11:39:11
我做错了什么?如何改进?我想每 5 秒从地图中删除一次。比如我加了ab0, ab1, ab2, ab3, ab4
。流应该删除 ab0, ab1, ab2
。由于地图中的元素大于MAX_SIZE
。然后我添加 abc0, abc1, abc2
。在删除第一个元素 5 秒后,应该删除 ab3, ab4, abc0
。但是,如您所见,所有项目都被同时删除。
What am I doing wrong?
和
Executors.newScheduledThreadPool(2).scheduleAtFixedRate(removerThread, 5, 5, TimeUnit.SECONDS);
,第一次执行时间会delayed秒5
秒,此时缓存包含:ab0 ab1 ab2 ab3 ab4 abc0 abc1 ab2
,前 5 个将被删除。你需要一个线程安全的版本
AbstractCustomCache customCache
因为它被多个线程修改。