`

java Thread

 
阅读更多
1.控制线程
1.1 join线程Thread类的三个重载方法
  public final void join() throws InterruptedException
  public final synchronized void join(long millis)
                        throws InterruptedException
  public final synchronized void join(long millis, int nanos)
                        throws InterruptedException
当某一个程序执行流调用一个线程的join()方法时,调用线程将会被阻塞直到被调用的线程执行完毕后才会继续执行阻塞的线程。第二个构造函数提供一个毫秒时间段,在给定的millis中调用线程没有执行完毕被阻塞的线程就不会再次等待它了开始执行程序代码块。
1.2 daemon线程(后台线程vs守护线程)
调用Thread类的如下方法:
public final void setDaemon(boolean on)
将会将该线程设置为后台线程。如果所有的线程都死亡了那么后台线程也自动死亡
判断线程是否是后台线程的方法:
public final boolean isDaemon()
1.3 线程睡眠
调用Thread类:
public static native void sleep(long millis) throws InterruptedException
public static void sleep(long millis, int nanos)
    throws InterruptedException
1.4 线程让步
yield方法:
Causes the currently executing thread object to temporarily pause
and allow other threads to execute.
public static native void yield()
线程优先级常量:
public final static int MIN_PRIORITY = 1
public final static int NORM_PRIORITY = 5
public final static int MAX_PRIORITY = 10
1.5 静态方法 yield 和 sleep 区别:
sleep: 让当前线程休眠让给其它线程执行不区分线程优先级
yield: 让当前线程休眠让给优先级和自身相同后者比自己高的线程才能执行。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics