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

本文详细介绍了Java中创建线程的五种方式,包括继承Thread类、实现Runnable接口以及使用匿名内部类和lambda表达式。同时,讲解了线程中断、线程等待、线程休眠和获取线程实例等基础知识,强调了线程中断的两种常见方法,并提供了代码示例。适合Java初学者和进阶者阅读。
摘要由CSDN通过智能技术生成

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

前言

这些内容都是小猪自己学习之后的总结的,如果哪里有错的,希望大佬指出哈~
在这里插入图片描述
那现在就进入正题吧!这篇博客要写的是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();
            }
        }
    }
}

(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();
            }
        }
    }
}

(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();
    }
}

(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();
    }
}

(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();
    }
}

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

二,线程中断

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

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

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

public volatile boolean isQuit = false

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

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

三,线程等待

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

四,线程休眠

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

五,获取线程实例

方法:

public static Thread currentThread();

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

Thread thread = Thread.currentThread();

在这里插入图片描述

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爱撸猫的程序媛

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值