建立线程有几种方式

整理一下,建立线程有三种方式: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

相关文章
相关标签/搜索