标签:demo hello string nts ini exec char rac create
Callable接口:
public interface Callable<V> {
    V call() throws Exception;
}
Runnable接口:
public interface Runnable {
    public abstract void run();
}
相同点:
不同点:
注意点:
Callable工作的Demo:
package com.callable.runnable;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
 * Created on Jason 2018/10/12.
 */
public class CallableImpl implements Callable<String> {
    public CallableImpl(String acceptStr) {
        this.acceptStr = acceptStr;
    }
    private String acceptStr;
    @Override
    public String call() throws Exception {
        // 任务阻塞 1 秒
        Thread.sleep(1000);
        return this.acceptStr + " append some chars and return it!";
    }
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<String> callable = new CallableImpl("my callable test!");
        FutureTask<String> task = new FutureTask<>(callable);
        long beginTime = System.currentTimeMillis();
        // 创建线程
        new Thread(task).start();
        // 调用get()阻塞主线程,反之,线程不会阻塞
        String result = task.get();
        long endTime = System.currentTimeMillis();
        System.out.println("hello : " + result);
        System.out.println("cast : " + (endTime - beginTime) / 1000 + " second!");
    }
}
测试结果:
hello : my callable test! append some chars and return it! cast : 1 second! Process finished with exit code 0
Runnable工作的Demo:
package com.callable.runnable;
/**
 * Created on Jason 2018/10/12.
 */
public class RunnableImpl implements Runnable {
    public RunnableImpl(String acceptStr) {
        this.acceptStr = acceptStr;
    }
    private String acceptStr;
    @Override
    public void run() {
        try {
            // 线程阻塞 1 秒,此时有异常产生,只能在方法内部消化,无法上抛
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 最终处理结果无法返回
        System.out.println("hello : " + this.acceptStr);
    }
    public static void main(String[] args) {
        Runnable runnable = new RunnableImpl("my runable test!");
        long beginTime = System.currentTimeMillis();
        new Thread(runnable).start();
        long endTime = System.currentTimeMillis();
        System.out.println("cast : " + (endTime - beginTime) / 1000 + " second!");
    }
}
测试结果:
cast : 0 second! hello : my runable test! Process finished with exit code 0
比较了两者后,发现Callable在很多特殊的场景下还是很有用的!最后留点代码,加深对Callable的认识!
package com.inte.fork;
/**
 * Created on Jason 2018/10/12.
 */
import java.util.*;
import java.util.concurrent.*;
import static java.util.Arrays.asList;
public class Sums {
    static class Sum implements Callable<Long> {
        private final long from;
        private final long to;
        Sum(long from, long to) {
            this.from = from;
            this.to = to;
        }
        @Override
        public Long call() {
            long acc = 0;
            for (long i = from; i <= to; i++) {
                acc = acc + i;
            }
            System.out.println(Thread.currentThread().getName() + " : " + acc);
            return acc;
        }
    }
    public static void main(String[] args) throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        List<Future<Long>> results = executor.invokeAll(asList(
                new Sum(0, 10), new Sum(0, 1_000), new Sum(0, 1_000_000)
        ));
        executor.shutdown();
        for (Future<Long> result : results) {
            System.out.println(result.get());
        }
    }
}
标签:demo hello string nts ini exec char rac create
原文地址:https://www.cnblogs.com/jasontec/p/9779342.html