public String GetKnnAsString(String featureVector) { // System.err.println(featureVector); // System.err.println(trainingVectors.size()); FeatureVector fv = new FeatureVector(featureVector, 32); PriorityBlockingQueue<CategoryDistances> pbq = new PriorityBlockingQueue<>(); ExecutorService pool = Executors.newFixedThreadPool(NumWorkers); String outp = ""; for (FeatureVector elem : trainingVectors) { pool.execute(new EuclideanWorker(elem, fv, pbq)); } pool.shutdown(); while (!pool.isTerminated()) {; } for (int i = 0; i < K; i++) { CategoryDistances cd = pbq.poll(); if (cd == null) { break; } outp += cd.toString() + VectorDelim; } // System.out.println(outp); return outp.substring(0, outp.length() - 1); }
@Override public Mission getMission(Sergeant s) { if (this.isEmpty()) return null; PriorityBlockingQueue<Mission> relevantQueue; Vector<Mission> temp = new Vector<Mission>(); if (s.getPriority().equals(Sergeant.SHORTEST)) relevantQueue = this.minLength; else if (s.getPriority().equals(Sergeant.LONGEST)) relevantQueue = this.maxLength; else if (s.getPriority().equals(Sergeant.MINITEMS)) relevantQueue = this.minItems; else relevantQueue = this.maxItems; while (!relevantQueue.isEmpty()) { Mission candidate = relevantQueue.poll(); temp.add(candidate); WarSim.LOG.fine("Testing Mission " + candidate.getName() + "(" + candidate.getSkill() + ")"); if (s.getSkills().contains(candidate.getSkill())) { for (Mission tm : temp) relevantQueue.put(tm); return candidate; } else WarSim.LOG.fine( "The sergeant " + s.getName() + " can't execute mission " + candidate.getName()); } for (Mission tm : temp) relevantQueue.put(tm); return null; }
private DataNodeIdentifier pickHeaviestLoad(DataNodeIdentifier otherNode) { DataNodeIdentifier replacementNode = null; PriorityBlockingQueue<DataNodeStatusPair> invertedStatusQueue = new PriorityBlockingQueue<DataNodeStatusPair>( datanodeStatuses.size(), new Comparator<DataNodeStatusPair>() { public int compare(DataNodeStatusPair a, DataNodeStatusPair b) { return -1 * a.compareTo(b); } }); for (DataNodeStatusPair each : datanodeStatuses) { invertedStatusQueue.put(each); } while (replacementNode == null) { DataNodeStatusPair next = invertedStatusQueue.poll(); DataNodeIdentifier nextId = next.getIdentifier(); if (!nextId.equals(otherNode)) { replacementNode = nextId; } } return replacementNode; }
public void run() { while (stillAlive.get()) { synchronized (this) { try { ScheduledJob job = schedule.peek(); if (job == null) { // If null, wake up every minute or so to see if there's something to do. // Most likely there will not be. try { this.wait(TIMEOUT_MS); } catch (InterruptedException e) { // interruption should occur when items are added or removed from the queue. } } else { // We've passed the job execution time, so we will run. if (!job.getScheduledExecution().isAfterNow()) { // Run job. The invocation of jobs should be quick. ScheduledJob runningJob = schedule.poll(); logger.info("Scheduler attempting to run " + runningJob.getId()); // Execute the job here try { executionManager.execute(runningJob.getId(), runningJob.isDependencyIgnored()); } catch (JobExecutionException e) { logger.info("Could not run job. " + e.getMessage()); } // Immediately reschedule if it's possible. Let the execution manager // handle any duplicate runs. if (runningJob.updateTime()) { schedule.add(runningJob); saveSchedule(); } else { // No need to keep it in the schedule. removeScheduledJob(runningJob); } } else { // wait until job run long millisWait = Math.max( 0, job.getScheduledExecution().getMillis() - (new DateTime()).getMillis()); try { this.wait(Math.min(millisWait, TIMEOUT_MS)); } catch (InterruptedException e) { // interruption should occur when items are added or removed from the queue. } } } } catch (Exception e) { logger.error("Unexpected exception has been thrown in scheduler", e); } catch (Throwable e) { logger.error("Unexpected throwable has been thrown in scheduler", e); } } } }
private DataNodeIdentifier pickReplacement( SegmentGroup affectedGroup, DataNodeIdentifier oldNode) { DataNodeIdentifier replacementNode = null; List<DataNodeStatusPair> removedPairs = new ArrayList<DataNodeStatusPair>(); while (replacementNode == null) { DataNodeStatusPair next = datanodeStatuses.poll(); removedPairs.add(next); DataNodeIdentifier nextId = next.getIdentifier(); if (!nextId.equals(oldNode) && !affectedGroup.isMember(nextId)) { replacementNode = nextId; } } for (DataNodeStatusPair each : removedPairs) { datanodeStatuses.add(each); } return replacementNode; }
public static void main(String[] args) { PriorityBlockingQueue<Event> queue = new PriorityBlockingQueue<Event>(); Thread[] taskThreads = new Thread[5]; // 初始这 5 个任务线程。 for (int i = 0; i < taskThreads.length; i++) { Task task = new Task(i, queue); taskThreads[i] = new Thread(task); } // 启动这 5 个任务线程。 for (Thread t : taskThreads) { t.start(); } // 使用 join() 方法,让 main 线程等待 5 个任务线程结束。 for (Thread t : taskThreads) { try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.printf("Main: Queue Size: %d\n", queue.size()); for (int i = 0; i < taskThreads.length * 1000; i++) { Event event = queue.poll(); System.out.printf("Thread %s: Priority %d\n", event.getThread(), event.getPriority()); } System.out.printf("Main: Queue Size: %d\n", queue.size()); System.out.printf("Main: End of the program\n"); }
private void handleMeasurement() { if (!userConsented()) { Logger.i("Skipping measurement - User has not consented"); return; } try { MeasurementTask task = taskQueue.peek(); // Process the head of the queue. if (task != null && task.timeFromExecution() <= 0) { taskQueue.poll(); Future<MeasurementResult> future; Logger.i("Processing task " + task.toString()); // Run the head task using the executor if (task.getDescription().priority == MeasurementTask.USER_PRIORITY) { sendStringMsg("Scheduling user task:\n" + task); // User task can override the power policy. So a different task wrapper is used. future = measurementExecutor.submit(new UserMeasurementTask(task)); } else { sendStringMsg("Scheduling task:\n" + task); future = measurementExecutor.submit(new PowerAwareTask(task, powerManager, this)); } synchronized (pendingTasks) { pendingTasks.put(task, future); } MeasurementDesc desc = task.getDescription(); long newStartTime = desc.startTime.getTime() + (long) desc.intervalSec * 1000; // Add a clone of the task if it's still valid. if (newStartTime < desc.endTime.getTime() && (desc.count == MeasurementTask.INFINITE_COUNT || desc.count > 1)) { MeasurementTask newTask = task.clone(); if (desc.count != MeasurementTask.INFINITE_COUNT) { newTask.getDescription().count--; } newTask.getDescription().startTime.setTime(newStartTime); submitTask(newTask); } } // Schedule the next measurement in the taskQueue task = taskQueue.peek(); if (task != null) { long timeFromExecution = Math.max(task.timeFromExecution(), Config.MIN_TIME_BETWEEN_MEASUREMENT_ALARM_MSEC); measurementIntentSender = PendingIntent.getBroadcast( this, 0, new UpdateIntent("", UpdateIntent.MEASUREMENT_ACTION), PendingIntent.FLAG_CANCEL_CURRENT); alarmManager.set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeFromExecution, measurementIntentSender); } } catch (IllegalArgumentException e) { // Task creation in clone can create this exception Logger.e("Exception when cloning task"); sendStringMsg("Exception when cloning task: " + e); } catch (Exception e) { // We don't want any unexpected exception to crash the process Logger.e("Exception when handling measurements", e); sendStringMsg("Exception running task: " + e); } persistState(); }