在Java多线程编程中,Thread类是其中一个核心和关键的角色。因此,对该类中一些基础常用方法的理解和熟练使用是开发多线程代码的基础。本篇主要总结一下Thread中常用的一些静态方法的含义及代码中的使用。

sleep方法

源码如下:

    /**      * Causes the currently executing thread to sleep (temporarily cease      * execution) for the specified number of milliseconds, subject to      * the precision and accuracy of system timers and schedulers. The thread      * does not lose ownership of any monitors.      *      * @param  millis      *         the length of time to sleep in milliseconds      *      * @throws  IllegalArgumentException      *          if the value of {@code millis} is negative      *      * @throws  InterruptedException      *          if any thread has interrupted the current thread. The      *          <i>interrupted status</i> of the current thread is      *          cleared when this exception is thrown.      */     public static native void sleep(long millis) throws InterruptedException;

可以看到sleep是一个静态的本地方法,因为是本地方法,所以并没有java代码的实现,其实是调用了底层的C库函数来实现的睡眠。

有一个long类型的参数,表示睡眠多少毫秒。

阅读注释,sleep方法的含义就是,让当前正在执行任务的线程睡眠(临时地停止执行)指定的毫秒数,这个精度和准确性是用系统时钟和调度器保证的。但是,线程并不会释放它拥有的锁。

注意该方法会抛出InterruptedException中断异常。

sleep不会释放锁代码示例:

public class ThreadsleepDemo{      private Object object = new Object();      public static void main(String[] args) {          ThreadsleepDemo threadsleepDemo = new ThreadsleepDemo();         Thread thread1 = threadsleepDemo.new SleepDemoThread();         thread1.setName("线程1");         Thread thread2 = threadsleepDemo.new SleepDemoThread();         thread2.setName("线程2");         thread1.start();         thread2.start();      }      class SleepDemoThread extends Thread{         @Override         public void run() {             synchronized (object){                 System.out.println(Thread.currentThread().getName());                 try {                     Thread.sleep(1000);                 } catch (InterruptedException e) {                     e.printStackTrace();                 }                 System.out.println(Thread.currentThread().getName());             }         }     } } 

输出结果如下:

线程1开始运行 线程1运行结束 线程2开始运行 线程2运行结束

可以多运行几次,可能会有线程1在上面或和线程2在上面,但始终都是一个行程运行完了才会运行另一个线程,中间不会插入进来一个线程运行。

yield方法

  /**      * A hint to the scheduler that the current thread is willing to yield      * its current use of a processor. The scheduler is free to ignore this      * hint.      *      * <p> Yield is a heuristic attempt to improve relative progression      * between threads that would otherwise over-utilise a CPU. Its use      * should be combined with detailed profiling and benchmarking to      * ensure that it actually has the desired effect.      *      * <p> It is rarely appropriate to use this method. It may be useful      * for debugging or testing purposes, where it may help to reproduce      * bugs due to race conditions. It may also be useful when designing      * concurrency control constructs such as the ones in the      * {@link java.util.concurrent.locks} package.      */     public static native void yield();

当前线程对调度器的一个暗示,表示愿意让出CPU执行器的当前使用权,但是调度器可以自由忽略这个提示。

Yeild是一种在可能会过度使用一个CPU的多个线程之间提升相对进度试探性尝试。它的使用应该结合详细的性能分析和基准测试来进行,确保它确实有预期的效果。

很少使用这种方法。 它可能对调试或测试有用,可能有助于根据竞态条件重现错误。 在设计并发控制结构(例如java.util.concurrent.locks包中的并行控制结构)时也可能有用。

join方法

join有三个重载的方法

join() join(long millis)     join(long millis,int nanoseconds)

主要看下第二个方法的源码