码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【JAVA】总结线程Thread的基本用法


        目录~~~

    1.线程创建

    2.线程中断

    3.线程等待

    4.线程休眠

    5.获取线程实例


    线程创建

    线程创建的方式有很多种,接下来介绍5种常见的线程创建方式

    1.实现Thread子类,重写run方法

    1. //实现Thread子类来创建线程(继承Thread)
    2. class MyThread extends Thread{
    3. @Override
    4. public void run() {
    5. while (true){
    6. System.out.println("hello world");
    7. try {
    8. Thread.sleep(1000);
    9. } catch (InterruptedException e) {
    10. throw new RuntimeException(e);
    11. }
    12. }
    13. }
    14. }
    15. public class Demo1 {
    16. public static void main(String[] args) throws InterruptedException {
    17. MyThread t = new MyThread();
    18. t.start();//当start开始调用,才算创建了线程
    19. while (true){
    20. System.out.println("hello main");
    21. Thread.sleep(1000);
    22. }
    23. }
    24. }

    2.实现Runnable接口,重写run方法

    1. //实现Runnable接口来创建线程
    2. class MyRunnable implements Runnable{
    3. @Override
    4. public void run() {
    5. while(true){
    6. System.out.println("hello hi");
    7. try {
    8. Thread.sleep(1000);
    9. } catch (InterruptedException e) {
    10. throw new RuntimeException(e);
    11. }
    12. }
    13. }
    14. }
    15. public class Demo2 {
    16. public static void main(String[] args) throws InterruptedException {
    17. MyRunnable runnable = new MyRunnable();
    18. Thread t = new Thread(runnable);//使用这个方法创建线程可以将线程与线程工作本身分开
    19. t.start();
    20. while (true){
    21. System.out.println("hello main");
    22. Thread.sleep(1000);
    23. }
    24. }
    25. }

    3.用匿名内部类来实现Thread子类

    1. //使用匿名内部类来实现Thread子类来创建线程
    2. public class Demo3 {
    3. public static void main(String[] args) throws InterruptedExcep
  • 相关阅读:
    Kotlin基础数据类型和运算符
    ceph15集群,虚拟机宕机1台
    数据库与数据仓库关联和区别
    总结如何申请注册 GitHub 教师教育优惠 Benefits for Teachers 来免费使用 copilot
    文未有福利 | 接口自动化你不懂?听HttpRunner的作者怎么说
    09【C语言 & 趣味算法】再识:折半查找(二分查找):基本思想、程序流程图及完整代码、附:顺序查找
    nacos回顾+cloud家族的皇家卫士Sentinel
    【Python 千题 —— 基础篇】列表倒转
    免费领取源码-小程序+spring boot流浪动物救助系统 12783
    水稻生物育种突破 国稻种芯-何登骥:功能性农业外源植物导入
  • 原文地址:https://blog.csdn.net/qq_61862008/article/details/126808595
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号