Thread 类

简介: Thread 类

hello,大家好,好久不见,甚是想念,今天为大家带来Thread类的相关知识点

🐷线程创建

🐷线程中断

🐷线程等待

🐷线程休眠

🐷获取线程实例


线程创建

线程创建有五种方式

1.继承Thread方法,重写run方法

2.实现runnable方法,重写run

3.继承Thread,使用匿名内部类

4.实现Runnable,使用匿名内部类

5.使用lambda表达式

这5个里面用的最多的还是第五个,下面我们一个一个来看

🎉继承Thread方法,重写run方法

package threading;
/**
 * Created with IntelliJ IDEA.
 * Description:演示多线程的基本创建方式
 * User: WHY
 * Date: 2022-09-02
 * Time: 9:46
 */
class MyThread extends Thread {//因为Thread这个类是标准库中的类,而自己写的MyThread 类继承了父类,所以要重写这个
    @Override
    public void run() {//重写run方法。run方法是thread父类里面已有的方法,此时重写他
        while (true) {
            System.out.println("hello  thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);//因为run方法重写了,所以在抛异常的时候不能选择用throws,只能try  catch;
            }
        }
    }
}
public class Demo1 {
    public static void main(String[] args) {
    MyThread t=new MyThread();//实例化的时候其实并没有启动线程
    t.start();//启动了这个线程
        while(true){
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}


2.实现runnable方法,重写run


package threading;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2022-09-03
 * Time: 21:38
 */
class MyRunnable implements Runnable{
    @Override
    public void run() {
        while (true) {
            System.out.println("hello thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
public class demo2 {
    //创建线程不仅可以用继承Thread,还有一种方法是实现Runnable接口,重写run
    public static void main(String[] args) {
        MyRunnable   runnable=new MyRunnable();
        Thread  t=new Thread(runnable);
        t.start();
        while(true){
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}


3.继承Thread,使用匿名内部类


public class demo1 {
    public static void main(String[] args) {
        System.out.println("hello main");
        new Thread(){
            @Override
            public void run() {
                System.out.println("hello thread");
                for(int i = 0;i<4;i++){
                    System.out.println("hello ");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("子线程运行结束");
            }
        }.start();
        for(int i = 0;i<4;i++){
            System.out.println("Main线程");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }   
        }
        System.out.println("线程已经结束了");
    }
}


4.实现Runnable,使用匿名内部类


public class demo {  
    public static void main(String[] args) {     
        // 创建一个线程,参数是实现了Runnable接口的匿名类的实例  
        Thread thread = new Thread(new Runnable() {   
            public void run() {   //实现run() 方法 
                System.out.println("hello thread");  
            }  
        });  
        thread.start();                         // 启动线程  
    }  
} 


5.使用lambda表达式


public class demo10 {
    public static void main(String[] args) {
        Thread t=new Thread(()->{
            while(!isQuit){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("线程执行完了");
        },"这是我的线程");
        t.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("设置让t线程结束");
    }
}


其中lambda表达式是最常用的那个

线程中断


package threading;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2022-09-11
 * Time: 14:43
 */
public class demo10 {
    public static boolean isQuit=false;
    public static void main(String[] args) {
        Thread t=new Thread(()->{
            while(!isQuit){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("线程执行完了");
        },"这是我的线程");
        t.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        isQuit=true;
        System.out.println("设置让t线程结束");
    }
}


package threading;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2022-09-11
 * Time: 14:57
 */
public class demo11 {
    public static void main(String[] args) {
        Thread t=new Thread(()->{
            while(!Thread.currentThread().isInterrupted()){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    //e.printStackTrace();
                    break;
                }
            }
            System.out.println("线程执行完了");
        },"这是我的线程");
        t.start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        t.interrupt();//主线程通过这个中断线程,设置标志位为true
        System.out.println("设置让t 结束");
    }
}


线程等待


public class demo12 {
    public static void main(String[] args) {
        Thread t=new Thread(()->{
            System.out.println("hello thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"这是我的代码");
        t.start();
        System.out.println("join之前");
        try {
            t.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("join之后");
    }
}


线程休眠

这个很简单没啥好说的,就是单纯地Thread.sleep();但是要抛出一个异常

线程创建一个实例


class Counter{
    public int  count;
    public  synchronized   void increase(){
        count++;
    }
}
public class demo14 {
    private static  Counter counter=new Counter();
    public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(()->{
            for(int i=0;i<50000;i++){
                counter.increase();
            }
        });
        Thread t2=new Thread(()->{
            for(int i=0;i<50000;i++){
                counter.increase();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("counter:"+counter.count);
    }
}


使用lambda表达式来创建一个t1,t2实例


public class demo10 {
    public static boolean isQuit=false;
    public static void main(String[] args) {
        Thread t=new Thread(()->{
            while(!isQuit){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("线程执行完了");
        },"这是我的线程");
        t.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        isQuit=true;
        System.out.println("设置让t线程结束");
    }
}


如上图代码,创建了t实例

今天的讲解就到此为止,我们下期再见,886!!!

相关文章
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16946 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
2天前
|
人工智能 安全 网络安全
|
6天前
|
云安全 数据采集 人工智能
|
7天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1250 2
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2338 7
|
18天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3808 22
|
19天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是DeepSeek AI开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、跑命令、处理表格、联网搜索等操作。免费开源,兼容Windows/macOS/Linux,提供桌面端与网页端,可灵活安装插件扩展能力。