All pastes #740901 Raw Edit

Vermischtes

public text v1 · immutable
·published 2007-10-18 10:19 UTC
rendered paste body
import Supercomputer.*;

public class App {
	public static void main(String[] args) {
		SumUpJob superJob = new SumUpJob(SupercomputerJob.INITIALJOB, 1, 10);
		SupercomputerJob[] jobs;
		try {
			jobs = superJob.partitionJob(5);
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
		
		AppThread[] threads  = new AppThread[jobs.length];
		for (int i=0; i<threads.length; ++i) {
			threads[i] = new AppThread(jobs[i]);
			threads[i].start();
		}
		
		boolean running;
		do {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			running = false;
			for (int i=0; i<threads.length; ++i) {
				if (threads[i].isAlive()) {
					running = true;
					break;
				}
			}
		} while (running);
		
		Object[] results = new Object[jobs.length];
		for (int i=0; i<threads.length; ++i) {
			results[i] = threads[i].getResult();
		}
		
		superJob.mergeAndDisplayJobResults(results);
	}
}

class AppThread extends Thread {
	private SupercomputerJob job;
	private Object[] result;
	
	public AppThread(SupercomputerJob job) {
		this.job = job;
	}
	
	public void run() {
		result = job.doTask();
	}
	
	public Object[] getResult() {
		return result;
	}
}