一、前言
定义服务接口Server、抽象服务类ThreadServer实现单线程任务服务对象代码示例,如实现DailyFileReaderServer日常文件读取服务线程类,详情参见代码示例。
二、代码示例
1.Server接口
public interface Server {@b@ @b@ public boolean isStart();@b@ @b@ public void start();@b@ @b@ public void stop();@b@}
2.抽象线程服务ThreadServer类
import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@@b@public abstract class ThreadServer implements Server, Runnable {@b@ @b@ protected static final Logger log = LoggerFactory.getLogger(ThreadServer.class);@b@@b@ private static final int MIN_INTERVAL = 100;@b@ @b@ private Thread thread = null;@b@ private volatile long interval = MIN_INTERVAL;@b@ private volatile boolean needStop = false;@b@ @b@ public long getInterval() {@b@ return interval;@b@ }@b@@b@ public void setInterval(long interval) {@b@ if (interval > MIN_INTERVAL) {@b@ this.interval = interval;@b@ }@b@ }@b@ @b@ @Override@b@ public boolean isStart() {@b@ return thread != null;@b@ }@b@@b@ @Override@b@ public synchronized void start() {@b@ if (thread == null) {@b@ needStop = false;@b@ thread = new Thread(this);@b@ thread.start();@b@ log.info("===> " + this.getClass().getSimpleName() + " start!");@b@ } else {@b@ log.info("===> " + this.getClass().getSimpleName() + " already start!");@b@ }@b@ }@b@@b@ @Override@b@ public synchronized void stop() {@b@ if (thread != null) {@b@ needStop = true;@b@ try {@b@ thread.join();@b@ thread = null;@b@ } catch (Exception e) {@b@ }@b@ log.info("===> " + this.getClass().getSimpleName() + " stop!");@b@ } else {@b@ log.info("===> " + this.getClass().getSimpleName() + " already stop!");@b@ }@b@ }@b@ @b@ @Override@b@ public void run() {@b@ long dt, start, end = -1;@b@ while (!needStop) {@b@ start = System.currentTimeMillis();@b@ dt = end == -1 ? 0 : start - end;@b@ try {@b@ update(dt);@b@ } catch (Exception e) {@b@ exceptionCaught(e);@b@ }@b@ end = System.currentTimeMillis();@b@ if (interval > 0) {@b@ dt = interval - end + start;@b@ if (dt > 0) {@b@ try {@b@ Thread.sleep(dt);@b@ } catch (Exception e) {@b@ }@b@ }@b@ }@b@ }@b@ }@b@@b@ public abstract void update(long dt) throws Exception;@b@ @b@ public void exceptionCaught(Exception e) {@b@ log.error("===> " + this.getClass().getName() + " run error: ", e);@b@ }@b@}
3.DailyFileReaderServer日常文件读取服务类
import java.io.File;@b@import java.util.ArrayList;@b@import java.util.Calendar;@b@import java.util.HashSet;@b@import java.util.List;@b@import java.util.Set;@b@@b@public class DailyFileReaderServer extends ThreadServer {@b@ @b@ public interface DailyFileReader {@b@ public String getFilePath(int date);@b@ public boolean readFile(int date, File file);@b@ }@b@ @b@ private int date;@b@ private long dateStartTime;@b@ private int offset;@b@ private Set<Integer> flags;@b@ private List<DailyFileReader> readers;@b@@b@ public DailyFileReaderServer(List<DailyFileReader> readers) {@b@ this.date = getCurrentDate();@b@ this.dateStartTime = getDateStartTime(date);@b@ @b@ this.offset = -1;@b@ this.flags = new HashSet<Integer>();@b@ this.readers = readers != null ? readers : new ArrayList<DailyFileReader>();@b@ }@b@ @b@ private int getCurrentDate() {@b@ Calendar ca = Calendar.getInstance();@b@ int year = ca.get(Calendar.YEAR);@b@ int month = ca.get(Calendar.MONTH) + 1;@b@ int day = ca.get(Calendar.DAY_OF_MONTH);@b@ return year * 10000 + month * 100 + day;@b@ }@b@ @b@ private long getDateStartTime(int date) {@b@ Calendar ca = Calendar.getInstance();@b@ ca.set(date/10000, (date%10000)/100, date%100, 0, 0, 0);@b@ return ca.getTimeInMillis();@b@ }@b@@b@ @Override@b@ public void update(long dt) throws Exception {@b@ int currentDate = getCurrentDate();@b@ if (date != currentDate) {@b@ offset = -1;@b@ date = currentDate;@b@ flags.clear();@b@ dateStartTime = getDateStartTime(date);@b@ }@b@ @b@ if (++offset < readers.size()) {@b@ if (flags.contains(offset) == false) {@b@ DailyFileReader reader = readers.get(offset);@b@ String path = reader.getFilePath(date);@b@ File file = new File(path);@b@ if (file.exists() && file.lastModified() > dateStartTime) {@b@ if (reader.readFile(date, file)) {@b@ flags.add(offset);@b@ }@b@ }@b@ }@b@ } else {@b@ offset = -1;@b@ }@b@ }@b@@b@}