重学Java之创建线程

Java 中创建线程有3 种方式:

  • 继承 Thread 类
  • 实现 Runnable 接口
  • 使用 Callable 和 FutureTask :FutureTask 本质上是 Runnable 接口,只是实现方式和 Runnable 不一样,所以单独放出来

继承Thread

编写一个 MyThread 类,继承 Thread ,重写 run() 方法,run() 方法就是具体要执行的任务。

调用 start() 方法来执行线程。

public class CreateThread {

    public static void main(String[] args) throws InterruptedException {
    	System.out.println("main() , " + Thread.currentThread().getName());
        MyThread thread = new MyThread();
        thread.start();
}

class MyThread extends Thread{

    @Override
    public void run() {
        super.run();
        System.out.println("MyThread [" + Thread.currentThread().getName() +"] running... ");
    }
}

运行结果,

main() , main
MyThread [Thread-0] running...

要看当前是哪个线程,调用 Thread.currentThread() 即可。

如果想自定义线程名字,可以加上构造函数 Thread(String name) ,

class MyThread extends Thread{

    public MyThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        super.run();
        System.out.println("MyThread [" + Thread.currentThread().getName() +"] running... ");
    }
}

新建线程时传入线程名称,

MyThread thread = new MyThread("A");
thread.start();

实现 Runnable 接口

编写一个类实现 Runnable 接口 ,重写 run() 方法。

把 Runnable 接口实例传给 Thread ,调用 Thread.start() 方法执行。

public class CreateThread {

    public static void main(String[] args) throws InterruptedException {
        Thread  thread2 = new Thread(new MyRunnable());
        thread2.run();
}

class MyRunnable implements Runnable{

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

使用 Callable 和 FutureTask

编写一个 MyCallable 类,实现 Callable 接口,重写 call() 方法,

创建 FutureTask 实例,把 MyCallable 类实例传给 FutureTask ,

把 FutureTask 实例作为参数传给 Thread 创建线程,

FutureTask 对象的 get() 方法来获得线程执行结束后的返回值。

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

public class CreateThread {

    public static void main(String[] args) throws InterruptedException {
        System.out.println("main() , " + Thread.currentThread().getName());
        MyCallable callable = new MyCallable();
        FutureTask<Integer> futureTask = new FutureTask<>(callable);

        new Thread(futureTask).start();
        System.out.println("futureTask.get():" + futureTask.get());
}

class MyCallable implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        System.out.println("MyCallable [" + Thread.currentThread().getName() + "] call ...");
        int k = 0;
        for (; k < 5; k++){

        }
        return k;
    }
}

运行结果,

main() , main
MyCallable [Thread-1] call ...
futureTask.get():5

FutureTask 本质上是 Runnable ,

// FutureTask
public class FutureTask<V> implements RunnableFuture<V> {

	public FutureTask(Callable<V> callable) {
        if (callable == null)
            throw new NullPointerException();
        this.callable = callable;
        this.state = NEW;       // ensure visibility of callable
    }
}


//RunnableFuture
public interface RunnableFuture<V> extends Runnable, Future<V> {
    /**
     * Sets this Future to the result of its computation
     * unless it has been cancelled.
     */
    void run();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值