Runnable 无返回值
Callable 有返回值
先看 Runnable
这里我直接新建的main方法来演示
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(50);
MyRunnable myRunnable = new MyRunnable();
System.out.println(new Date());
for (int i = 0; i < 50; i++) {
executorService.execute(myRunnable);
}
System.out.println(new Date());
}
ExecutorService 是线程的service对象,Executors.newFixedThreadPool(50);表示这个线程池中最多可以同时运行50个线程,这里可以根据实际业务情况做出调整;
同时我们要新建MyRunnable类,这里面写的是我们需要多线程去运行的代码
public class MyRunnable implements Runnable {
Runnable与Callable的区别

最低0.47元/天 解锁文章
1536

被折叠的 条评论
为什么被折叠?



