• 【Java第22期】:Thread 类的基本用法


    小猪又回来了~
    其实我一直都在,哈哈哈,只是没什么时间写博客
    那现在就总结一下自己最近的做学吧!
    在这里插入图片描述

    前言

    这些内容都是小猪自己学习之后的总结的,如果哪里有错的,希望大佬指出哈~
    在这里插入图片描述
    那现在就进入正题吧!这篇博客要写的是Thread类的基本用法

    一,线程的创建

    我们都知道使用最多的线程创建的方法是使用lambda表达式创建,那现在我们来看看其他的方法吧!这里有五种线程创建的方法~
    (1)继承Thread ,重写run
    例如:

    class  MyThread extends Thread{
        @Override
        public void run() {
            while (true) {
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                }catch (InterruptedException e ){
                    e.printStackTrace();
                }
            }
        }
    }
    public class Demo21 {
        public static void main(String[] args) {
            Thread t = new Thread();
            t.start();
            t.run();
            while (true) {
                System.out.println("hello main");
                try {
                    Thread.sleep(1000);
                }catch (InterruptedException e ){
                    e.printStackTrace();
                }
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    (2)实现 Runnable, 重写 run
    例子:

    class MyRunnable implements Runnable{
        @Override
        public void run() {
            while (true){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                }catch (InterruptedException e ){
                    e.printStackTrace();
                }
            }
        }
    }
    
    public class Demo22 {
        public static void main(String[] args) {
            Runnable runnable = new MyRunnable();
            Thread t = new Thread(runnable);
            t.start();
            while (true){
                System.out.println("hello main");
                try {
                    Thread.sleep(1000);
                }catch (InterruptedException e ){
                    e.printStackTrace();
                }
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    (3)继承 Thread, 重写 run, 使用匿名内部类
    例子:

    public class Demo23 {
        public static void main(String[] args) {
            Thread t = new Thread(){
                @Override
                public void run() {
                    while (true){
                        System.out.println("hello thread");
                        try {
                            Thread.sleep(1000);
                        }catch (InterruptedException e ){
                            e.printStackTrace();
                        }
                    }
                }
            };
            t.start();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    (4)实现 Runnable, 重写 run, 使用匿名内部类
    例子:

    public class Demo24 {
        public static void main(String[] args) {
                Thread t = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (true) {
                            System.out.println("hello thread");
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
                t.start();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    (5)使用 lambda 表达式
    例子:

    public class Demo25 {
        public static void main(String[] args) {
            Thread t = new Thread(() ->{
                while (true){
                    System.out.println("hello thread");
                    try {
                        Thread.sleep(1000);
                    }catch (InterruptedException e ){
                        e.printStackTrace();
                    }
                }
            });
            t.start();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    以上就是五种创建线程的方法以及例子
    总的来说,分为两大类:继承Thread类和实现Runnable接口。

    二,线程中断

    目前常见的线程中断有两种方法:通过共享的标志来进行沟通和通过调用interrupt()方法来通知

    1,使用自定义的变量来作为标志位

    这个操作需要给关键字上加volatile
    例子:

    public volatile boolean isQuit = false
    
    • 1

    2,使用Thread.interrupted()或者Thread.currentThread().isInterrupted 代替自定义标志位。

    其中Thread内部包含了一个Boolean类型的变量作为线程是否被中断的标志,如下:
    在这里插入图片描述
    (如果想要代码示例的可以来私我哦~)

    三,线程等待

    等待线程的方法:
    在这里插入图片描述
    上面的代码中也有用到,这里就不详细举例了~

    四,线程休眠

    方法和说明:
    在这里插入图片描述
    上面代码中也用过了~就不详细讲了!

    五,获取线程实例

    方法:

    public static Thread currentThread();
    
    • 1

    说明:
    返回当前线程的对象引用
    代码实例:

    Thread thread = Thread.currentThread();
    
    • 1

    在这里插入图片描述

    以上内容只是简单的讲述,如果又哪里你有疑惑但这里没讲到的,欢迎来骚扰哈~
    拜~
    记得关注小猪哦!
    请添加图片描述

  • 相关阅读:
    TSINGSEE视频智能分析系统AI算法针对遛狗不拴绳行为的监管方案
    数据结构(八)排序
    SpringCloud对服务内某个client进行单独配置
    Docker基本命令
    【CAN】CAN基础概念4
    数据库——JDBC基本连接步骤
    linux(centos7)常用命令 开启关闭防火墙
    vue实战入门后台篇六:springboot+mybatis实现网站后台-前端登录功能对接
    公网访问内网中Wsl2服务器(借助frp)
    Mybatis的mapper.xml批量插入、修改sql
  • 原文地址:https://blog.csdn.net/m0_62262008/article/details/126019613