java 多线程(二)

线程的生命周期

(1)新建
(2)就绪
(3)运行
(4)阻塞
(5)结束

休眠

sleep()的调用可以抛出InterruptedException异常,异常不能跨线程传播,所以必须在本地处理所有任务内部产生的异常。
在java.lang.Thread中定义,如果在main方法中直接调用,表示主线程休眠;一般在run方法中通过判断条件调用。

public static native void sleep(long millis) throws InterruptedException;
public static void sleep(long millis, int nanos)throws InterruptedException {
      if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
      }
      if (nanos < 0 || nanos > 999999) {
            throw new IllegalArgumentException("nanosecond timeout value out of range");
      }
      if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
            millis++;
      }
      sleep(millis);
}
  • millis:毫秒
  • nanos:毫微秒,十亿分之一秒

获取当前线程

//Thead.currentThread()获取
public static native Thread currentThread();

优先级

public final static int MIN_PRIORITY = 1;
public final static int NORM_PRIORITY = 5;
public final static int MAX_PRIORITY = 10;
private int priority;
public final void setPriority(int newPriority) {
    ThreadGroup g;
    checkAccess();
    if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
        throw new IllegalArgumentException();
    }
    if((g = getThreadGroup()) != null) {
        if (newPriority > g.getMaxPriority()) {
            newPriority = g.getMaxPriority();
        }
        setPriority0(priority = newPriority);
    }
}
public final int getPriority() {
    return priority;
}

让步

public static native void yield();

作用是提醒线程调度器,该换个线程来执行了。

后台线程

后台线程也称为守护线程,是指在程序运行的时候在后台为其它线程提供服务的线程,比如gc线程和main线程。
任何非后台线程在运行,后台线程就不会终止。反之,当所有非后台线程结束,后台线程也终止。
后台线程创建的任何线程默认都为后台线程。

//true表示设置为后台线程,false表示非后台线程,但必须在线程启动前调用
public final void setDaemon(boolean on) {
    checkAccess();
    if (isAlive()) {
        throw new IllegalThreadStateException();
    }
    daemon = on;
}
private boolean daemon = false;
//判断是否是一个后台线程
public final boolean isDaemon() {
    return daemon;
}

加入一个线程

如果某个线程在另一个线程t上调用t.join(),此线程将被挂起,直到目标线程t结束才恢复(即t.isAlive()返回为假)

public final synchronized void join(long millis)throws InterruptedException {
      long base = System.currentTimeMillis();
      long now = 0;
      if (millis < 0) {
           throw new IllegalArgumentException("timeout value is negative");
      }
      if (millis == 0) {
            while (isAlive()) {
                  wait(0);
            }else {
                  while (isAlive()) {
                        long delay = millis - now;
                        if (delay <= 0) {
                        break;
                  }
                  wait(delay);
                  now = System.currentTimeMillis() - base;
            }
      }
}
public final synchronized void join(long millis, int nanos)throws InterruptedException {
      if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
      }
      if (nanos < 0 || nanos > 999999) {
            throw new IllegalArgumentException("nanosecond timeout value out of range");
      }
      if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
            millis++;
      }
      join(millis);
}
public final void join() throws InterruptedException {
    join(0);
}

捕获异常

posted @ 2020-10-16 17:25  黑白猫123  阅读(102)  评论(0编辑  收藏  举报