总结

package com.sxh.gaoji;

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

/**
 * @author 司小虎 E-mail:sxh100600@gmail.com
 * @version 创建时间:2020/1/4 15:57
 */
//总结多线程
public class ZongJie {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        new MyThread().start();

        new Thread(new MyRunnable()).start();

        //另一种启动Callable线程
        FutureTask<Integer> futureTask=new FutureTask<Integer>(new MyCallable());
        new Thread(futureTask).start();
        System.out.println(futureTask.get());

    }
}
//继承Thread
class MyThread extends Thread{
    @Override
    public void run() {
        System.out.println("我是MyThread");
    }
}
//实现Runnable接口
class MyRunnable implements Runnable{
    @Override
    public void run() {
        System.out.println("我是MyRunnable");
    }
}
//实现Callable接口
class MyCallable implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        System.out.println("我是MyCallable");
        return 100;
    }
}

运行结果

Last modification:February 6th, 2020 at 04:47 pm
如果觉得我的文章对你有用,请随意赞赏