JavaEE-什么是多线程?(Thread类的基本用法)

Thread 类的常见构造方法

以下展示的是Thread类的常见构造方法

class  MyRunnable implements Runnable{
    @Override
    public void run() {
   }
}
        Thread t1=new Thread();                    //创建线程对象
        Thread t2 = new Thread(new MyRunnable());  //使用 Runnable 对象创建线程对象
        Thread t3 =new Thread(new MyRunnable(),"线程3"); //使用 Runnable 对象创建线程对象,并命名
        Thread t4 = new Thread("线程4");           //创建线程对象,并命名

启动一个线程

通过重写run方法,创建一个线程对象,但线程创建出来后并没有运行起来,得通过start方法进行线程启动。

class MyThread extends Thread{
    @Override
    public void run() {
        System.out.println("新线程");
    }
}
public class test4 {
    public static void main(String[] args) {
        Thread t=new MyThread();
        t.start();                    //线程开始运行
    }
}

这将创建一个新的线程并执行t的run()方法中的代码。需要注意的是,一旦线程t被启动,它将独立于原始线程运行,并且可能会在不同的处理器上并行执行。

中断一个线程

中断线程通常有两种方法:

通过共享的标记来进行中断

public class ThreadDemo2 {
    static class MyRunnable1 implements Runnable{
        public static boolean isQuit=false;
        @Override
        public void run() {
            while (!isQuit){
                System.out.println("新创建的线程");
            }
        }
    }
    public static void main(String[] args) {
        MyRunnable1 target =new MyRunnable1();
Thread t =new Thread(target);
t.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
target.isQuit=true;
    }
}

调用 interrupt() 方法来进行中断

可以使用Thread类的interrupt()方法来中断一个线程。当调用一个线程的interrupt()方法时,它会设置该线程的中断标志位,并允许其他代码检查该标志位并作出相应的响应。
interrupt()方法是判断当前线程的中断标志位是否设置,调用后清除标志位。

public class ThreadDemo6 {
    public static class MyRunnable implements Runnable{
        @Override
        public void run() {
            while (!Thread.interrupted()){
                System.out.println("新线程");
            }
        }
    }
    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();
        Thread t=new Thread(myRunnable);
        t.start();
        try {
            Thread.sleep(3*1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        t.interrupt();
    }
}

这将设置线程t的中断标志位,使得其他代码可以检查该标志位并作出相应的响应。具体来说,其他代码可以使用Thread类的isInterrupted()方法来检查线程是否被中断,以及使用Thread类的interrupted()方法来获取线程的中断状态。
如果其他代码需要响应中断请求,可以在适当的地方添加逻辑来处理中断事件。例如,可以在run()方法中添加一个循环,每次循环都检查线程是否被中断,并在被中断时退出循环。

等待一个线程

可以使用Thread类的join()方法来等待一个线程执行完毕。当调用一个线程的join()方法时,当前线程会进入该线程的等待队列,直到被等待的线程执行完毕才会继续执行。

public class ThreadDemo4 {
    public static void main(String[] args) {
        Thread t = new Thread(()->{
            System.out.println("新线程");
        });
        t.start();
        try {
            t.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

mian线程等待新创建的t线程执行完后,main线程才能结束。
除了使用join()方法外,还可以使用其他方法来等待线程执行完毕,例如使用Object类的wait()方法和notify()方法来实现线程间的通信和协调。但是,这些方法需要更复杂的编程模型和更多的代码,因此通常建议使用join()方法来等待线程执行完毕。

获取当前线程的引用

在Java中,可以使用Thread类的currentThread()方法来获取当前线程的引用。这个方法返回一个Thread对象,代表正在执行的线程。

public class ThreadDemo5 {
    public static void main(String[] args) {
        Thread t =Thread.currentThread();
        System.out.println(t.getName());
    }
}

在这里插入图片描述
通过返回mian线程的引用,打印main线程的名称。

休眠线程

下面我们通过调用System.currentTimeMillis()方法来查看休眠主线程一定时间后当前时间的变化与休眠时间是否相同

public class ThreadDemo {
public static void main(String[] args) throws InterruptedException {
System.out.println(System.currentTimeMillis());   //以毫秒为单位返回当前时间
Thread.sleep( 1000);
System.out.println(System.currentTimeMillis());   //以毫秒为单位返回当前时间
}
}

在这里插入图片描述
在这里插入图片描述
所以,这个方法只能保证实际休眠时间是大于等于参数设置的休眠时间的。需要了解更多并发知识请阅读我的并发专栏

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

手插口袋谁也不爱♡

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

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

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

打赏作者

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

抵扣说明:

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

余额充值