Thread类的基本用法

1.线程创建

①继承Thread类

创建一个自己的类去继承Thread类,并且重写run方法,run方法是改线程的入口。

class MyThread extends Thread{
    @Override
    public void run() {
        System.out.println("hello Thread");
    }
}

public class ThreadDemo1 {
    public static void main(String[] args) {
        Thread t = new MyThread();
        t.start();
    }
}

 ②实现Runnable接口

创建实现了 Runnable 接口的类对象,创建线程对象,传入 Runnable 对象,启动线程。

class MyThread3 implements Runnable{
    @Override
    public void run() {
            System.out.println("hello runnable");
    }
}
public class ThreadDemo3 {
    public static void main(String[] args) {
        Runnable runnable = new MyThread3();
        Thread t = new Thread(runnable);
        t.start();
        System.out.println("hello main");
    }
}

2.线程中断

线程中断是一种协作机制,允许一个线程请求另一个线程停止正在进行的操作。可以通过调用 interrupt() 方法来中断线程的执行。

package thread;
public class ThreadDemo13 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            while(!Thread.currentThread().isInterrupted()){
                System.out.println("我是一个线程,正在工作中");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                   e.printStackTrace();
                    break;
                }
            }
            System.out.println("线程执行完毕!");
        });
        t.start();
        Thread.sleep(3000);
        System.out.println("让t线程结束");
        t.interrupt();
    }
}

线程在执行过程中可以通过检查 Thread.currentThread().isInterrupted() 方法来判断是否被中断,并根据需要终止自己的执行。 

3.线程等待

线程等待是一种同步机制,用于让一个线程等待另一个线程完成其执行。

package thread;
import java.util.Random;
public class ThreadDemo14 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            Random random = new Random();
            int n = 5;
            for(int i=0;i <5 ;i++){
                System.out.println("我是一个线程,正在工作中。。。");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("线程执行结束");
        });
        t.start();
        t.join();
        System.out.println("这是主线程,期望这个日志在t结束之后打印");
    }
}

 这种机制常用于协调多个线程的执行顺序或者等待某个条件满足。

4.线程休眠

线程休眠是让当前线程暂停执行一段时间的一种方式。

try {
    Thread.sleep(1000); 
} catch (InterruptedException e) {
    e.printStackTrace();
}

  Thread.sleep(1000) 让当前线程休眠1秒,这在需要等待一段时间后再继续执行的场景中非常有用。

5.获取线程实例

可以通过 Thread.currentThread() 方法获取当前正在执行的线程实例。

package thread;

public class ThreadDemo17 {
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            Thread t = Thread.currentThread();
            System.out.println(t.getName());
        });

        Thread t2 = new Thread(()->{
            Thread t = Thread.currentThread();
            System.out.println(t.getName());
        });

        t1.start();
        t2.start();
    }
}

Thread.currentThread() 返回一个代表当前线程的 Thread 对象,通过这个对象可以获取当前线程的信息,比如线程名称、状态等。

总结

通过掌握以上 Thread 类的基本用法,包括创建线程、中断、等待、休眠和获取线程实例等操作,可以更有效地进行多线程编程。合理地使用这些方法可以提高程序的并发性能,并确保线程安全和正确的线程协作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值