Mercurial > hg > Members > sugi > javafx
view src/alice/topology/manager/keeparive/PingScheduler.java @ 32:e580cd6b225a
bug fixed (about totalTime)
author | sugi |
---|---|
date | Mon, 11 Nov 2013 16:25:55 +0900 |
parents | 5598fa745a51 |
children | f5cf57c645ab |
line wrap: on
line source
package alice.topology.manager.keeparive; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import alice.codesegment.CodeSegment; public class PingScheduler extends CodeSegment{ private LinkedList<TaskInfo> list = new LinkedList<TaskInfo>(); private HashMap<String, Integer> map = new HashMap<String, Integer>(); private long INTERVAL = 60 * 1000; public boolean updateFlag = false; private boolean postponeFlag = false; private boolean nowTaskFlag = false; public long time = 0; public int taskNum = 0; public TaskInfo nowTask; private long totalTime = 0; public long getTotalTime(){ return totalTime; } public List<TaskInfo> getTaskList(){ return list; } public synchronized void postpone(String name){ long postponeTime = nowTask.getTime() - (System.currentTimeMillis() - time); if (name.equals(nowTask.getName())){ TaskInfo nextTask = list.get(0); nextTask.setTime(postponeTime + nextTask.getTime()); nowTaskFlag = true; } else { for (int cnt = 0 ;cnt < list.size(); cnt++){ TaskInfo task = list.get(cnt); if (name.equals(task.getName())){ if (cnt+1 < list.size()){ TaskInfo tasknext = list.get(cnt+1); tasknext.setTime(task.getTime()); } list.remove(cnt); task.setTime(INTERVAL); addTask(task); break; } } } postponeFlag = true; notify(); } public synchronized void addTask(TaskInfo newInfo){ if (map.containsKey(newInfo.getName())){ newInfo.setTaskNum(map.get(newInfo.getName())); } else { taskNum +=10; map.put(newInfo.getName(), taskNum); newInfo.setTaskNum(taskNum); } if (list.isEmpty()) { list.add(newInfo); } else { for (int cnt = 0; cnt < list.size(); cnt++){ TaskInfo info = list.get(cnt); if (info.getTime() < newInfo.getTime()){ newInfo.setTime(newInfo.getTime() - info.getTime()); if (cnt+1 == list.size()){ list.add(newInfo); break; } } else if (info.getTime() == newInfo.getTime()){ newInfo.setTime(newInfo.getTime() - info.getTime()); list.add(cnt+1, newInfo); break; } else if (info.getTime() > newInfo.getTime()){ info.setTime(info.getTime() - newInfo.getTime()); list.add(cnt, newInfo); break; } } } updateFlag = true; } @Override public synchronized void run() { try { while(true){ if(list.size()== 0)System.exit(0); nowTask = list.poll(); System.out.print("taskNum "+nowTask.getTaskNum()); System.out.println(" taskTime "+nowTask.getTime()); time = System.currentTimeMillis(); if (nowTask.getTime()!=0) this.wait(nowTask.getTime()); if (postponeFlag){ long remainingTime = nowTask.getTime() - (System.currentTimeMillis() - time); totalTime += (System.currentTimeMillis() - time); if (nowTaskFlag){ nowTask.setTime(INTERVAL); addTask(nowTask); } else { nowTask.setTime(remainingTime); list.add(0, nowTask); updateFlag = true; } postponeFlag = false; } else { totalTime +=nowTask.getTime(); nowTask.setTime(INTERVAL); addTask(nowTask); } } } catch (InterruptedException e) { } } }