FutureTask的使用

实例1:

package com.cn.test2;

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

public class FutureTaskSample {

	static FutureTask<String> future = new FutureTask(new Callable<String>() {
		public String call() {
			try {
				Thread.sleep(3000) ;
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "testPageContent and provide that the operation is a time exhausted thing...";
		}
	});

	public static void main(String[] args) throws InterruptedException,
			ExecutionException {
		// Start a thread to let this thread to do the time exhausting thing
		new Thread(future).start();

		// Main thread can do own required thing first
		doOwnThing();

		// At the needed time, main thread can get the result
		System.out.println(future.get());
	}

	public static String doOwnThing() {
		return "Do Own Thing";
	}

}


实例2
package com.cn.test2;

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

public class FutureTaskDemo {  
    public static void main(String[] args) {  
    	
    
        // 初始化一个Callable对象和FutureTask对象  
        Callable pAccount = new PrivateAccount();  
        FutureTask futureTask = new FutureTask(pAccount);  
        // 使用futureTask创建一个线程  
        Thread pAccountThread = new Thread(futureTask);  
        System.out.println("futureTask线程现在开始启动,启动时间为:" + System.nanoTime());  
        pAccountThread.start();  
        
        System.out.println("主线程开始执行其他任务");  
        // 从其他账户获取总金额  
        int totalMoney = new Random().nextInt(100000);  
        System.out.println("现在你在其他账户中的总金额为" + totalMoney);  
        System.out.println("等待私有账户总金额统计完毕...");  
        
        // 测试后台的计算线程是否完成,如果未完成则等待  
        while (!futureTask.isDone()) {  
            try {  
                Thread.sleep(500);  
                System.out.println("私有账户计算未完成继续等待...");  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
        }  
        System.out.println("futureTask线程计算完毕,此时时间为" + System.nanoTime());  
        Integer privateAccountMoney = null;  
        try {  
            privateAccountMoney = (Integer) futureTask.get();  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        } catch (ExecutionException e) {  
            e.printStackTrace();  
        }  
        System.out.println("您现在的总金额为:" + totalMoney + privateAccountMoney.intValue());  
    }  
}  
  
@SuppressWarnings("all")  
class PrivateAccount implements Callable {  
    Integer totalMoney;  
  
    @Override  
    public Object call() throws Exception {  
        Thread.sleep(5000);  
        totalMoney = new Integer(new Random().nextInt(10000));  
        System.out.println("您当前有" + totalMoney + "在您的私有账户中");  
        return totalMoney;  
    }  
  
}  

猜你喜欢

转载自wuzhaohuixy-qq-com.iteye.com/blog/2145819
今日推荐