⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pendingjob.java

📁 javaP2P技术内幕课程111213141516源代码
💻 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 + -