Callable实现多线程(有返回值)

编程

package com.ego.item.test;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.FutureTask;

public class CallableDemo {

public static void main(String[] args) throws ExecutionException, InterruptedException {

FutureTask<String> stringFutureTask = new FutureTask<>(new java.util.concurrent.Callable<String>() {

@Override

public String call() throws Exception {

for (int i = 0; i < 100; i++) {

System.out.println("线程1--->" + i);

}

return "success---1";

}

});

FutureTask<String> stringFutureTask2 = new FutureTask<>(new java.util.concurrent.Callable<String>() {

@Override

public String call() throws Exception {

for (int i = 0; i < 100; i++) {

System.out.println("线程2--->" + i);

}

return "success---2";

}

});

FutureTask<String> stringFutureTask3 = new FutureTask<>(new java.util.concurrent.Callable<String>() {

@Override

public String call() throws Exception {

for (int i = 0; i < 100; i++) {

System.out.println("线程3--->" + i);

}

return "success---3";

}

});

new Thread(stringFutureTask).start();

new Thread(stringFutureTask2).start();

new Thread(stringFutureTask3).start();

System.out.println("线程1 执行返回结果: " + stringFutureTask.get());

System.out.println("线程2 执行返回结果: " + stringFutureTask2.get());

System.out.println("线程3 执行返回结果: " + stringFutureTask3.get());

}

}

以上是 Callable实现多线程(有返回值) 的全部内容, 来源链接: utcz.com/z/515644.html

回到顶部