?? pendingjob.java
字號:
/* * Created by IntelliJ IDEA. * User: fsommers * Date: May 1, 2002 * Time: 11:38:39 PM * To change template for new class use * Code Style | Class Templates options (Tools | IDE Options). */package primecruncher;import java.util.HashSet;import java.util.Iterator;import java.util.Arrays;public class PendingJob { private String jobID; private Dispatcher dispatcher; private ResultListener resultListener; private HashSet jobSet = new HashSet(); PendingJob(String jobID, Dispatcher dispatcher, ResultListener resultListener) { this.jobID = jobID; this.dispatcher = dispatcher; this.resultListener = resultListener; } void addJob(DispatcherJob job) { JobResult r = new JobResult(job); jobSet.add(r); System.out.println("Added new job to jobset"); } String getID() { return jobID; } ResultListener getResultListener() { return resultListener; } /** * A dispatcher job calls back to indicate that it got a result. We need to iterate * through all jobs to see if we are complete with all the jobs. */ void gotResult(DispatcherJob job, Result result) { System.out.println("Got a result"); HashSet jobCopy = null; //this only clones the set, not the elements of the set synchronized(jobSet) { jobCopy = (HashSet)jobSet.clone(); } Iterator it = jobCopy.iterator(); while (it.hasNext()) { JobResult rs = (JobResult)it.next(); if (rs.dispatcherJob.equals(job)) { rs.setResult(result); break; } } areWeReady(); } /** * Iterate through the set, and see if we every element is ready. */ private void areWeReady() { HashSet s; synchronized(jobSet) { s = (HashSet)jobSet.clone(); } Iterator it = s.iterator(); while (it.hasNext()) { JobResult r = (JobResult)it.next(); if (!r.isRead()) { return; } } //now everything is ready, terminate all jobs, call back to Dispatcher JobResult[] results = (JobResult[])s.toArray(new JobResult[s.size()]); Arrays.sort(results); Result[] sortedResults = new Result[results.length]; for (int i=0; i < sortedResults.length; i++) { sortedResults[i] = results[i].getResult(); } dispatcher.jobComplete(this, sortedResults); } public boolean equals (Object o) { if (o instanceof PendingJob) { PendingJob p = (PendingJob)o; return jobID.equals(p.jobID); } return false; } public int hashCode() { return jobID.hashCode(); } /** * Bundle a job's result as well as status */ class JobResult implements Comparable { DispatcherJob dispatcherJob; Result result; boolean ready = false; /** * Comparator is based on dispatcherJob's count */ public int compareTo(Object o) { if (o instanceof JobResult) { JobResult r = (JobResult)o; return dispatcherJob.compareTo(r.dispatcherJob); } return 0; } JobResult(DispatcherJob dispatcherJob) { this.dispatcherJob = dispatcherJob; } boolean isRead() { return ready; } void setResult(Result result) { this.result = result; ready = true; } Result getResult() { return result; } public boolean equals(Object o) { if (o instanceof JobResult) { JobResult jr = (JobResult)o; return dispatcherJob.equals(jr.dispatcherJob); } return false; } public int hashCode() { return dispatcherJob.hashCode(); } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -