Section 5 Implementing the Callable interface

Java 5.0 provides a new
way of creating execution threads in java.util.concurrent: Callable interface
⚫ Callable interface is similar to Runnable, both are designed for those classes whose instances may be executed by another thread. But the Runnable doesn't return a result and cannot throw checked exceptions.
⚫ Callable needs to depend on FutureTask, and FutureTask can also be used as a lock.

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

/*
 * 一、创建执行线程的方式三:实现 Callable 接口。 相较于实现 Runnable 接口的方式,方法可以有返回值,并且可以抛出异常。
 * 
 * 二、执行 Callable 方式,需要 FutureTask 实现类的支持,用于接收运算结果。  FutureTask 是  Future 接口的实现类
 */
public class TestCallable {
    
    
	
	public static void main(String[] args) {
    
    
		ThreadDemo td = new ThreadDemo();
		
		//1.执行 Callable 方式,需要 FutureTask 实现类的支持,用于接收运算结果。
		FutureTask<Integer> result = new FutureTask<>(td);
		
		new Thread(result).start();
		
		//2.接收线程运算后的结果
		try {
    
    
			Integer sum = result.get();  //FutureTask 可用于 闭锁
			System.out.println(sum);
			System.out.println("------------------------------------");
		} catch (InterruptedException | ExecutionException e) {
    
    
			e.printStackTrace();
		}
	}

}

class ThreadDemo implements Callable<Integer>{
    
    

	@Override
	public Integer call() throws Exception {
    
    
		int sum = 0;
		
		for (int i = 0; i <= 100000; i++) {
    
    
			sum += i;
		}
		
		return sum;
	}
	
}

/*class ThreadDemo implements Runnable{

	@Override
	public void run() {
	}
	
}*/

Guess you like

Origin blog.csdn.net/m0_37294838/article/details/127683820