JavaEE学习笔记(多线程)

本文详细介绍了Java中线程的创建、中断、等待和休眠。通过实例展示了继承Thread类、实现Runnable接口、使用匿名内部类以及Lambda表达式创建线程的方法。此外,还探讨了如何通过标志位和Thread类的interrupt方法来中断线程,并利用join方法实现线程间的等待控制。最后,解释了Thread.sleep的作用和线程休眠的原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

Thread类的基本使用

一.线程的创建

二.线程的中断

三.线程的等待

四.线程的休眠

五.获取线程实例


Thread类的基本使用

一.线程的创建

线程的创建方法有五类。

1.继承Thread类创建线程

public class MyThread extends Thread{
    @Override
    public void run() {
        while(true){
            System.out.println("hello thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.getStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        myThread.start();

        while(true){
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.getStackTrace();
            }
        }
    }
}

2.实现Runnable接口创建线程

public class MyRunable implements Runnable{
    @Override
    public void run() {
        while(true){
            System.out.println("hello runnable");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.getStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Thread t = new Thread(new MyRunable());
        t.start();

        while(true){
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.getStackTrace();
            }
        }
    }
}

3.继承Thread类使用匿名内部类创建线程

public class demo3 extends Thread{
    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.getStackTrace();
                    }
                }
            }
        };
        t.start();
    }
}

4.实现Runnable接口使用匿名内部类创建线程

public class demo4{
    public static void main(String[] args) {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    System.out.println("hello runnable");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.getStackTrace();
                    }
                }
            }
        });
        t.start();
    }
}

5.使用Lambda表达式创建线程

public class Lambda {
    public static void main(String[] args) {
        Thread t = new Thread(()->{
           while (true){
               System.out.println("hello Lambda");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.getStackTrace();
               }
           }
        });
        t.start();
    }
}

二.线程的中断

我们都知道什么时候线程断了,那就是run方法执行完了,有没有什么方法可以让线程提前中断呢?这个时候我们可以自己设置一个标志位,控制标志位从而控制线程什么时候中断。

public class demo5 {
    private 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.getStackTrace();
               }
           }
        });
        t.start();

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.getStackTrace();
        }
        //将isQuit改为true则t线程里的while循环就进不去从而结束线程
        isQuit = true;
        System.out.println("终止t线程");
    }
}

其实在Thread里有自带的标志位interrupt,所以我们就可以这样使用

public class demo6 {
    public static void main(String[] args) {
        Thread t = new Thread(()->{
           while(!Thread.currentThread().isInterrupted()){
               //Thread.interruted()这是一个静态方法 Thread.currentThread().isInterrupted()这是一个实例方法,
               // 其中currentThread能够获取到当前线程的实例,而isInterrupted()就是判断标志位的
               System.out.println("hello thread");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.getStackTrace();
                   System.out.println("这是收尾工作");
                   break;
               }
           }
        });
        t.start();

        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.getStackTrace();
        }
        t.interrupt();
    }
}

三.线程的等待

由于线程的随机调度,我们时常会看到两个线程一下你先运行一下我先运行的情况

 而我们讨厌随机性,所以有没有什么办法可以让我们控制线程之间的调度呢?

有的那就是join方法,他可以控制线程结束的顺序。

如果我们在main线程里使用了t.join(),那表达的就是等到t线程结束才开始main线程,期间main线程进行堵塞(不参与cpu的调度)。

public class demo7 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                System.out.println("hello thread");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.getStackTrace();
                }
            }
        });
        t.start();
        t.join();

        for (int i = 0; i < 10; i++) {
            System.out.println("hello main");
            Thread.sleep(500);
        }

    }
}

 我们也可以在join里设置一个时间,表示等到这个时间我就不等了

public class demo7 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                System.out.println("hello thread");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.getStackTrace();
                }
            }
        });
        t.start();
        t.join(3000);

        for (int i = 0; i < 10; i++) {
            System.out.println("hello main");
            Thread.sleep(500);
        }

    }
}

四.线程的休眠

线程的休眠sleep说的简单点就是,有两个队列一个就绪队列,一个阻塞队列,若一个线程调用了sleep的话则进入阻塞队列,等到阻塞的时间到了,则进入就绪队列等待cpu的调度(而不是立刻执行)。

五.获取线程实例

public static Thread currentThread();

获取当前这个线程对应的Thread对象的引用,那个线程调用这个方法得到的就是那个线程的引用

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

子健变于晏

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

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

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

打赏作者

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

抵扣说明:

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

余额充值