线程常见方法

线程常见方法

1)public void start():启动线程,调用run()方法。

2)public void run():线程的入口点,包含线程要执行的操作。

3)public void stop():停止线程的执行。已过时,不推荐使用。

4)public void suspend():挂起线程。已过时,不推荐使用。

5)public void resume():恢复被挂起的线程。已过时,不推荐使用。

6)public void interrupt():中断线程。相当于设置线程的中断状态。(只是改变状态,并不会使线程停止)

  • 打断 sleep,wait,join 线程会清空打断状态

    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t.start();
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        t.interrupt();
        System.out.println(t.isInterrupted()); // false
    }
    
  • LockSupport.park() 可用来阻塞当前线程,在其他线程中使用interrupt()可唤醒该线程

    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            log.info("unpark");
            LockSupport.park();
            log.info("park");
            log.info("{}",Thread.currentThread().isInterrupted());
        });
        t.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        t.interrupt();
    }
    

7)public static boolean interrupted():测试当前线程是否被中断,并清除中断状态。

8)public boolean isInterrupted():测试线程是否被中断。

9)public final void setPriority(int newPriority):设置线程的优先级。

10)public final int getPriority():返回线程的优先级。

11)public final void setName(String name):设置线程的名称。

12)public final String getName():返回线程的名称。

13)public final void setDaemon(boolean on):设置线程为守护线程或用户线程。

  • 守护线程会在所有非守护线程执行完后强制关闭

    public static void main(String[] args) {
        Long start = System.currentTimeMillis();
        Thread t = new Thread(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        t.setDaemon(true);
        t.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        Long end = System.currentTimeMillis();
        System.out.println(end - start); // 1000
    }
    

14)public final boolean isDaemon():判断线程是否为守护线程。

15)public final void join() throws InterruptedException:等待该线程终止。

16)public final void join(long millis) throws InterruptedException:等待该线程终止,最多等待指定的毫秒数。

17)public final void sleep(long n) throws InterruptedException :让当前执行的线 程休眠n毫秒,休眠时让出CPU的时间片给其它 线程

18)public final void yield():提示线程调度器,让出当前线程对 CPU的使用,主要是为了测试和调试

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java中多线程常用的方法有以下几种: 1. 继承Thread类:创建一个继承自Thread类的子类,并重写run()方法,在run()方法中定义线程要执行的任务。然后通过创建子类的对象,调用start()方法启动线程。 2. 实现Runnable接口:创建一个实现了Runnable接口的类,并实现其run()方法,在run()方法中定义线程要执行的任务。然后通过创建该类的对象,将其作为参数传递给Thread类的构造方法,再调用start()方法启动线程。 3. 使用Callable和Future:Callable接口是一种带有返回值的线程,通过实现Callable接口并实现其call()方法来定义线程要执行的任务。然后使用ExecutorService的submit()方法提交Callable任务,并返回一个Future对象,通过Future对象可以获取线程执行的结果。 4. 使用线程池:通过Executor框架提供的线程池来管理线程的创建和执行。可以使用Executors类提供的静态方法创建不同类型的线程池,然后将任务提交给线程池执行。 5. 使用synchronized关键字:通过在方法或代码块前加上synchronized关键字来实现线程同步,保证多个线程对共享资源的访问是互斥的。 6. 使用Lock接口:Lock接口提供了比synchronized更灵活和强大的线程同步机制。通过Lock接口的lock()和unlock()方法来实现对共享资源的加锁和解锁。 7. 使用wait()、notify()和notifyAll()方法:通过Object类提供的wait()、notify()和notifyAll()方法来实现线程间的通信和协作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

林小果呀

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

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

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

打赏作者

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

抵扣说明:

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

余额充值