Java教程

线程池回顾

本文主要是介绍线程池回顾,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package cn.ruhsang.gaoji;

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

//回顾线程池的创建
public class ThreadNew {
    public static void main(String[] args) {
        //1.第一个方法的调用
        new MyThread1().start();
        //2.第二个方法的调用
        MyThread2 myThread2 = new MyThread2();
        new Thread(myThread2).start();
        //3.第三个方法的启动
        FutureTask<Integer> futureTask = new FutureTask<Integer>(new MyThread3());
        new Thread(futureTask).start();

        try {
            Integer integer = futureTask.get();
            System.out.println(integer);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


    }
}
//1.继承Thread类
class MyThread1 extends Thread{
    @Override
    public void run() {
        System.out.println("MyThread1");
    }
}
//2.实现Runnable接口
class MyThread2 implements Runnable{
    @Override
    public void run() {
        System.out.println("MyThread2");
    }
}
//3.实现callable接口
class MyThread3 implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        System.out.println("MyThread3");
        return 100;
    }
}
这篇关于线程池回顾的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!