📄 uploadreaper.java
字号:
package com.redmoon.kit.util;
import java.util.Vector;
import java.util.Iterator;
import org.apache.log4j.Logger;
import java.util.Map;
import java.util.Collections;
import java.util.Set;
import java.io.IOException;
import java.io.File;
public class UploadReaper extends Thread {
Logger logger = Logger.getLogger(UploadReaper.class.getName());
private static long reapInterval = 1000*60*60*6; // 每隔6小时刷新一次
private static UploadReaper uploadReaper = null;
private final long expireTimeLen = 1000*60*60*6; // 超期时长为6小时
/**
* Creates a new CacheTimer object. The currentTime of Cache will be
* updated at the specified update interval.
*
* @param updateInterval the interval in milleseconds that updates should
* be done.
*/
public UploadReaper(long reapInterval) {
this.reapInterval = reapInterval;
// Make the timer be a daemon thread so that it won't keep the VM from
// shutting down if there are no other threads.
this.setDaemon(true);
// Start the timer thread.
start();
}
public static long getReapInterval() {
return reapInterval;
}
/**
* 单态模式
* @param updateInterval long
*/
public static synchronized void initInstance(long reapInterval) {
if (uploadReaper==null)
uploadReaper = new UploadReaper(reapInterval);
}
public void run() {
// Run the timer indefinetly.
while (true) {
// 遍历链表,如果有超期的文件则将其线程所对应的文件块删除(一个线程对应于一个文件块,一个文件块在上传时由多个Segment组成)
// 并将其从链表中删除
Map m = UploadDdxc.uploadFileInfos;
Set s = m.keySet(); // Needn't be in synchronized block
Vector expireUfis = new Vector();
synchronized (m) { // Synchronizing on m, not s!
Iterator i = s.iterator(); // Must be in synchronized block
while (i.hasNext()) {
UploadFileInfo ufi = (UploadFileInfo) i.next();
if (System.currentTimeMillis() - ufi.getReceiveTime() >
expireTimeLen) {
// 记录将删除的key
expireUfis.addElement(ufi.getFileId());
// 删除线程及相关文件
Vector v = ufi.getUploadThreadInfos();
Iterator ir = v.iterator();
while (ir.hasNext()) {
UploadThreadInfo uti = (UploadThreadInfo) ir.next();
// 删除文件块(每个线程对应一个文件块)
String blockPath = FileUpload.getTmpPath() +
ufi.getBlockName(uti.getBlockId());
File file = new File(blockPath);
if (!(file.exists()))
file.delete();
}
}
}
// 从链表中删除超期的FileUploadInfo
Iterator ir = expireUfis.iterator();
while (ir.hasNext()) {
String fileId = (String)ir.next();
UploadDdxc.uploadFileInfos.remove(fileId);
System.out.println("UploadReaper run: Delete UploadFileInfo " + fileId);
}
}
try {
sleep(reapInterval);
}
catch (InterruptedException ie) { }
}
}
public static synchronized UploadReaper getInstance() {
if (uploadReaper==null)
initInstance(reapInterval);
return uploadReaper;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -