建立線程有幾種方式

整理一下,建立線程有三種方式:ide

  • 繼承 Thread 重寫 run 方法;
  • 實現 Runnable 接口;
  • 實現 Callable 接口。

一、繼承Thread類spa

class ttt extends Thread{
    @Override
    public void run() {
        super.run();
    }
}

new ttt().start();線程

二、實現Runnable接口繼承

class t implements Runnable{

    @Override
    public void run() {
        System.out.println("======");
    }
}

new t().start();接口

三、實現Callable接口,能夠返回值io

class tt implements Callable<String>{

    @Override
    public String call() throws Exception {
        return "zhangsan";
    }
}

Stirng result = new tt().call();class

相關文章
相關標籤/搜索