jdk1.6中添加的future

摘自:http://caterpillar.onlyfun.net/Gossip/JavaGossip-V2/CallableFuture.htm
package onlyfun.caterpillar;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

public class PrimeCallable implements Callable<int[]> {
	private int max;

	public PrimeCallable(int max) {
		this.max = max;
	}

	public int[] call() throws Exception {
		int[] prime = new int[max + 1];

		List<Integer> list = new ArrayList<Integer>();

		for (int i = 2; i <= max; i++)
			prime[i] = 1;

		for (int i = 2; i * i <= max; i++) {
			if (prime[i] == 1) {
				for (int j = 2 * i; j <= max; j++) {
					if (j % i == 0)
						prime[j] = 0;
				}
			}
		}

		for (int i = 2; i < max; i++) {
			if (prime[i] == 1) {
				list.add(i);
			}
		}

		int[] p = new int[list.size()];
		for (int i = 0; i < p.length; i++) {
			p[i] = list.get(i).intValue();
		}

		return p;
	}
}

package onlyfun.caterpillar;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class FutureDemo {
	public static void main(String[] args) {
		Callable<int[]> primeCallable = new PrimeCallable(1000);
		FutureTask<int[]> primeTask = new FutureTask<int[]>(primeCallable);

		Thread t = new Thread(primeTask);
		t.start();

		try {
			Thread.sleep(5000);

			if (primeTask.isDone()) {
				int[] primes = primeTask.get();
				for (int prime : primes) {
					System.out.print(prime + " ");
				}
				System.out.println();
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}
}

你可能感兴趣的:(java,thread,.net,J#)