Callable创建多线程
生活随笔
收集整理的這篇文章主要介紹了
Callable创建多线程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import java.util.concurrent.Callable;/*** @author silence* Callable創建多線程* 創件一個類,實現 Callable 《》 這里填返回值類型* 重寫call 方法。*/
public class MyCallable implements Callable<String> {@Overridepublic String call() throws Exception {for (int i = 0; i < 100; i++) {System.out.print("callable:");System.out.print(Thread.currentThread().getName()+"--");System.out.println(i);}return "callable執行完了";}
}
========================
import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask;public class Demo1 {public static void main(String[] args) throws ExecutionException, InterruptedException {MyCallable callable1 = new MyCallable();FutureTask futuretask = new FutureTask(callable1);//創建多線程Thread thread = new Thread(futuretask);//啟動多線程thread.start();//獲取返回值String o = (String) futuretask.get();System.out.println(o);} }總結
以上是生活随笔為你收集整理的Callable创建多线程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 单机斗地主,单机斗地主
- 下一篇: 模拟注册用户,按照以下要求实现相关功能: