(八)Callable和Runnable的区别

本专栏多线程目录:

(一)线程是什么

(二)Java线程与系统线程和生命周期

(三)Java线程创建方式

(四)为什么要使用线程池

(五)四种线程池底层详解

(六)ThreadPoolExecutor自定义线程池

(七)线程池的大小如何确定

(八)Callable和Runnable的区别

(九)线程池异常捕获

(十)线程池参数——workQueue用法

(十一)sleep(1)、sleep(0)和sleep(1000)的区别

(十二)yield、notify、notifyAll、sleep、join、wait的区别

(十三)synchronized用法,四种锁范围

(十四)volatile的用法,原子性问题

(十五)ThreadLocal的用法,如何解决内存泄漏

(十六)ReentrantLock可重入锁使用和介绍

(十七)AtomicInteger原子类的介绍和使用

(十八)Worker线程管理


Callable和Runnable都是一个接口。

Runnable

@FunctionalInterface
public interface Runnable {
   
   
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}

Runnable只有一个run方法,在使用普通线程的时候,我们可以实现Runnable接口即可,Thread类在调用start()函数后就是执行的是Runnable的run()函数。

简单使用:

 Thread thread =new Thread(new TestRunnable());
 thread.start();

Runnable的run方法没有返回值。

demo:

public class ThreadPoolALL {
   
   
    public static void main(String[] args) {
   
   
        new ThreadPoolALL().ExecutorThreadPool();
    }

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

醋酸菌HaC

请我喝杯奶茶吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值