java多线程一、通过继承类或实现接口三种创建线程的方法

java可以通过继承Thread、实现Runnable接口、实现Callable接口来实现多线程

  • 因为继承Thread实现Runnable用FutureTask包装Callable本质上都是Runnable的实例,所以都可以用new Thread(Runnable target).start()来启动线程。
  • 继承Thread实现Runnable不能返回结果,用FutureTask包装Callable可以返回结果。

Thread 实现了 Runnable 接口,本质上还是一个 Runnable 接口

public class Thread implements Runnable {
	public Thread(Runnable target) {...}
	public synchronized void start() {...} // 启动线程
	...
}

通过继承 Thread 类来创建线程

public class ThreadInstance extends Thread {
    String message;

    public ThreadInstance(String message) {
        this.message = message;
    }

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

    public static void main(String[] args) {
        Runnable threadInstance = new ThreadInstance("hello world");
        new Thread(threadInstance).start();
    }
}

Runnable接口

@FunctionalInterface
public interface Runnable {
    public abstract void run();
}

通过实现Runnable接口重写run()来创建线程:

public class RunnableInstance implements Runnable {
    String message;

    public RunnableInstance(String message) {
        this.message = message;
    }

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

    public static void main(String[] args) {
        Runnable runnableInstance = new ThreadInstance("hello world");
        new Thread(runnableInstance).start();
		// new Thread(() -> System.out.println("hello world")).start(); lambda写法
    }
}

Callable 接口

@FunctionalInterface
public interface Callable<V> {
    V call() throws Exception;
}

FutureTask 类,本质上还是Runnable,所以可以使用 Thread(Runnable target).start() 来启动线程

public class FutureTask<V> implements RunnableFuture<V> {
    public FutureTask(Callable<V> callable) {
        ...
    }

    public FutureTask(Runnable runnable, V result) {
        ...
    }
}


public interface RunnableFuture<V> extends Runnable, Future<V> {
    void run();
}

public interface Future<V> {
    boolean cancel(boolean mayInterruptIfRunning);

    boolean isCancelled();

    boolean isDone();
    
    V get() throws InterruptedException, ExecutionException;

    V get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException;
}

通过实现Callable接口重写call()来创建线程:

public class CallableInstance implements Callable<Integer> {
    @Override
    public Integer call() {
        System.out.println("hello world");
        return 6;
    }

    public static void main(String[] args) {
        CallableInstance callableInstance = new CallableInstance();
        FutureTask<Integer> integerFutureTask = new FutureTask<>(callableInstance);
        new Thread(integerFutureTask).start();

        try {
            System.out.println(integerFutureTask.get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值