多线程常用方法

多线程常用方法

currentThread

Thread.currentThread();获取当前线程
Thread.currentThread().getName();获取当前线程名称

setName/getName

thread.setName(“test1”);设置线程名
thread.getName();获取线程名

isAlive

thread.isAlive();线程是否存活

sleep

Thread.sleep(1000);线程睡眠1秒

getId

thread.getId();获取线程唯一ID
该线程结束后线程ID可能会被重新使用

yield

Thread.yield();放弃CPU执行权,其他线程执行完成后才会执行

setPriority

thread.setPriority();设置优先级0-10,默认5
优先级设置不当会导致线程饥饿无法运行,不建议设置优先级

interrup

thread.interrupt();设置线程中断标志
设置线程中断标志并不能使线程中断,可以在执行过程中获取中断标志然后硬编码停止业务。interrupt()会中断wait()并抛出异常。

public class NewThread extends Thread {
    @Override
    public void run() {
        for (int i=0;;i++){
             //获取中断标志
            if(this.isInterrupted()){
                System.out.println("中断标志为true,我要退出");
                break;
            }
            System.out.println("-------->"+i);
        }
    }
}
 public static void main(String[] args) throws InterruptedException {
        NewThread thread=new NewThread();
        thread.start();
        Thread.sleep(10);//主线程睡眠10毫秒
        //设置中断标志
        thread.interrupt();
    }

setDaemon

thread.setDaemon(true);设置线程为守护线程
在Java中线程分为用户线程和守护线程,守护线程是为其他线程服务的,垃圾回收器就是典型的守护线程,当jvm中没有用户线程时守护线程机会自动销毁,jvm就会退出

public class NewThread extends Thread {
    @Override
    public void run() {
        for (int i=0;;i++){
            System.out.println("-------->"+i);
        }
    }
}
   public static void main(String[] args) throws InterruptedException {
        NewThread thread=new NewThread();
        thread.setDaemon(true);//设置线程为守护线程
        thread.start();
        Thread.sleep(10);
    }

wait/notify/notifyAll

锁对象.wait()该方法必须写在同步代码块中,由锁对象调用释放锁进入等待队列,当锁对象执行notify()或notifyAll()时,才会结束等待继续执行。当存在多个等待线程时notify()只能随机的唤醒其中的一个。notifyAll()可以唤醒所有线程。wait(long)在设置时间内未被唤醒就会自动唤醒。notify/notifyAll不会立即唤醒线程,当notify/notifyAll的同步代码块执行结束后才会唤醒线程,因此notify/notifyAll一般放在同步代码块的最后。

public class WaitThread extends Thread {
    private Object lock;
    public WaitThread(Object lock) {
        this.lock = lock;
    }
    @Override
    public void run() {
        synchronized (lock){
            System.out.println(Thread.currentThread().getName()+"线程开始等待");
            try {
                lock.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"线程结束等待");
        }
    }
}

public class TestThread {
    public static void main(String[] args) throws InterruptedException {
        Object o = new Object();
        WaitThread waitThread = new WaitThread(o);
        WaitThread waitThread2 = new WaitThread(o);
        WaitThread waitThread3 = new WaitThread(o);
        waitThread.setName("test1");
        waitThread2.setName("test2");
        waitThread3.setName("test3");
        waitThread.start();
        waitThread2.start();
        waitThread3.start();
        Thread.sleep(3000);
        synchronized (o){
           // o.notify();//随机唤醒一个线程
            o.notifyAll();//唤醒所有线程
        }
    }
}

join

1.t.join()方法会使所有线程都暂停并等待t的执行完毕后再执行
2.join之所以可以实现线程等待是因为调用wait方法
3.wait方法会让当前线程陷入等待。注意,是当前线程!
4.join源码中,只会调用wait方法,并没有在结束时调用notify,这是因为线程在die的时候会自动调用自身的notifyAll方法,来释放所有因为该锁陷入等待的资源和锁

 Thread t1 = new Thread(() -> {
            System.out.println("线程" + Thread.currentThread().getName() + "----------执行");
            try {
                Thread.sleep(3000);
                System.out.println("线程" + Thread.currentThread().getName() + "-------执行结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "1");

        Thread t2 = new Thread(() -> {
            System.out.println("线程" + Thread.currentThread().getName() + "----------执行");
        }, "2");
        t1.start();
        t1.join();
        t2.start();
        System.out.println("-------------------------------------主线程结束");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

桀骜浮沉

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

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

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

打赏作者

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

抵扣说明:

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

余额充值