砍材农夫砍材农夫
  • java
  • redis
  • mysql
  • 场景类
  • vuepress搭建
  • hexo搭建
  • 常用工具

    • git
    • gradle
    • Zadig
    • it-tools
    • 开源推荐
    • curl
  • 大前端

    • nodejs
    • npm
    • webpack
    • 微信
    • 正则
    • uniapp
  • java

    • java基础
    • jdk体系
    • jvm
    • spring
    • spring_cloud
    • spring_boot
    • 分库分表
    • zookeeper
  • python

    • python基础
    • python高级
    • python框架
  • 算法

    • 算法
  • 网关

    • spring_cloud_gateway
    • openresty
  • 高可用

    • 秒杀
    • 分布式
    • 缓存一致
  • MQ

    • MQ
    • rabbitMQ
    • rocketMQ
    • kafka
  • 其它

    • 设计模式
    • 领域驱动(ddd)
  • 关系型数据库

    • mysql5.0
    • mysql8.0
  • 非关系型数据库

    • redis
    • mongoDB
  • 分布式/其他

    • ShardingSphere
    • 区块链
  • 向量数据库

    • M3E
    • OPEN AI
  • Jmeter
  • fiddler
  • wireshark
  • AI入门
  • AI大模型
  • AI工具
  • MCP
  • AI集成框架
  • 相关算法
  • gitee
  • github
  • infoq
  • osc
  • 砍材工具
  • 关于
  • 相关运营
  • docker
  • k8s
  • devops
  • nginx
  • 元宇宙
  • 区块链
  • 物联网
  • linux
  • webrtc
  • web3.0
  • gitee
  • github
  • infoq
  • osc
  • 砍材工具
  • 关于
  • 中考
  • 投资
  • 保险
  • java
  • redis
  • mysql
  • 场景类
  • vuepress搭建
  • hexo搭建
  • 常用工具

    • git
    • gradle
    • Zadig
    • it-tools
    • 开源推荐
    • curl
  • 大前端

    • nodejs
    • npm
    • webpack
    • 微信
    • 正则
    • uniapp
  • java

    • java基础
    • jdk体系
    • jvm
    • spring
    • spring_cloud
    • spring_boot
    • 分库分表
    • zookeeper
  • python

    • python基础
    • python高级
    • python框架
  • 算法

    • 算法
  • 网关

    • spring_cloud_gateway
    • openresty
  • 高可用

    • 秒杀
    • 分布式
    • 缓存一致
  • MQ

    • MQ
    • rabbitMQ
    • rocketMQ
    • kafka
  • 其它

    • 设计模式
    • 领域驱动(ddd)
  • 关系型数据库

    • mysql5.0
    • mysql8.0
  • 非关系型数据库

    • redis
    • mongoDB
  • 分布式/其他

    • ShardingSphere
    • 区块链
  • 向量数据库

    • M3E
    • OPEN AI
  • Jmeter
  • fiddler
  • wireshark
  • AI入门
  • AI大模型
  • AI工具
  • MCP
  • AI集成框架
  • 相关算法
  • gitee
  • github
  • infoq
  • osc
  • 砍材工具
  • 关于
  • 相关运营
  • docker
  • k8s
  • devops
  • nginx
  • 元宇宙
  • 区块链
  • 物联网
  • linux
  • webrtc
  • web3.0
  • gitee
  • github
  • infoq
  • osc
  • 砍材工具
  • 关于
  • 中考
  • 投资
  • 保险
  • 基础面试题

    • 基础面试
    • 双亲委派
    • 关于锁
  • 线程并发

    • 线程
    • 线程安全
    • 线程实现方式
    • 线程相关属性
    • Thread和Object相关方法
    • 线程异常
    • 线程池
  • 高并发(JUC|AQS)

    • concurrency
    • AbstractQueuedSynchronizer
    • CountDownLatch
    • Semaphore
    • CyclicBarrier
    • ReentrantLock
    • Future/FutureTask
    • Fork/Join
    • BlockingQueue
    • ArrayBlockingQueue
    • DelayQueue
    • PriorityBlockingQueue
  • 集合相关

    • 集合相关
  • jvm

    • jvm概览
    • 垃圾标记算法
    • 垃圾回收算法
    • jvm监控相关工具
    • jvm内存调优
  • 锁

    • synchronized
  • 线程实现方式
    • 继承Thread类
    • 实现Runnable接口
    • 使用Callable接口(伪)
    • 使用线程池(伪)
    • 匿名类(伪)
  • 线程启动方式
  • 正确的停止线程

线程实现方式

继承Thread类

public class MyThread extends Thread{ 
    public void run(){ 
        System.out.println("hello world");
    }
}
public static void main(String[] args) { 
    MyThread t = new MyThread();
    t.start();
}

实现Runnable接口

public class MyThread implements Runnable{ 
    public void run(){ 
        System.out.println("hello world");
    }
}
public static void main(String[] args) { 
    MyThread t = new MyThread();
    new Thread(t).start();
}

使用Callable接口(伪)

public class MyThread implements Callable<String>{ 
    public String call(){ 
        return "hello world";
    }
}
public static void main(String[] args) throws Exception{ 
    MyThread t = new MyThread();
    FutureTask<String> task = new FutureTask<>(t);
    new Thread(task).start();
    System.out.println(task.get());
    System.out.println(task.isDone());
    System.out.println(task.isCancelled());
    System.out.println(task.cancel(true));
}

使用线程池(伪)

public static void main(String[] args) { 
    ExecutorService executor = Executors.newCachedThreadPool();
    executor.execute(new Runnable() { 
        public void run() { 
            System.out.println("hello world");
        }
    })
    executor.shutdown();
}

匿名类(伪)

public static void main(String[] args) { 
    new Thread(){ 
        public void run() { 
            System.out.println("hello world");
        }
    }
    .start();
}

线程启动方式

正确的停止线程

最近更新: 2026/1/6 21:35
Contributors: kcnf
Prev
线程安全
Next
线程相关属性