多线程常用方法
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("-------------------------------------主线程结束");