Object.wait notify notifyAll native

来源:互联网 发布:淘宝老店新开流量限制 编辑:程序博客网 时间:2024/05/20 19:16

native 关键字,表示该函数是原生函数,是用c/c++实现的,被编译成了DLL,java只能调用,函数体都在DLL中,不能查看源码,实际上java就是通过不同的操作系统实现这些原生函数从而跨平台的,这些原生函数是其它语言实现的,例如c++

这些方法都是object的方法
void notifyAll()
解除所有那些在该对象上调用wait方法的线程的阻塞状态。该方法只能在同步方法或同步块内部调用。如果当前线程不是锁的持有者,该方法抛出一个IllegalMonitorStateException异常。
void notify()
随机选择一个在该对象上调用wait方法的线程,解除其阻塞状态。该方法只能在同步方法或同步块内部调用。如果当前线程不是锁的持有者,该方法抛出一个IllegalMonitorStateException异常。
void wait()
导致线程进入等待状态,直到它被其他线程通过notify()或者notifyAll唤醒。该方法只能在同步方法中调用。如果当前线程不是锁的持有者,该方法抛出一个IllegalMonitorStateException异常。
void wait(long millis)和void wait(long millis,int nanos)
导致线程进入等待状态直到它被通知或者经过指定的时间。这些方法只能在同步方法中调用。如果当前线程不是锁的持有者,该方法抛出一个IllegalMonitorStateException异常。
Object.wait()和Object.notify()和Object.notifyall()必须写在synchronized方法内部或者synchronized块内部,这是因为:这几个方法要求当前正在运行object.wait()方法的线程拥有object的对象锁。

Thread.join方法
public class ThreadJoin {static class CustomThread1 extends Thread {public CustomThread1() {super("[CustomThread1] Thread");};public void run() {String threadName = Thread.currentThread().getName();System.out.println(threadName + " start.");try {for (int i = 0; i < 5; i++) {System.out.println(threadName + " loop at " + i);Thread.sleep(10000);}System.out.println(threadName + " end.");} catch (Exception e) {System.out.println("Exception from " + threadName + ".run");}}}static class CustomThread extends Thread {CustomThread1 t1;public CustomThread(CustomThread1 t1) {super("[CustomThread] Thread");this.t1 = t1;}public void run() {String threadName = Thread.currentThread().getName();System.out.println(threadName + " start.");try {t1.join();System.out.println(threadName + " end.");} catch (Exception e) {System.out.println("Exception from " + threadName + ".run");}}}public static void main(String[] args) {String threadName = Thread.currentThread().getName();System.out.println(threadName + " start.");CustomThread1 t1 = new CustomThread1();CustomThread t = new CustomThread(t1);try {t1.start();Thread.sleep(20000);t.start();t.join();//main线程会在这里等到t线程结束后才继续执行} catch (Exception e) {System.out.println("Exception from main");}System.out.println(threadName + " end!");}}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);// 表示获取object的锁,一直等待下去,直到该线程结束或者被唤醒            }        } else {            while (isAlive()) {                long delay = millis - now;                if (delay <= 0) {                    break;                }                wait(delay);                now = System.currentTimeMillis() - base;            }        }    }


输出结果为:


main start.
[CustomThread1] Thread start.
[CustomThread1] Thread loop at 0
[CustomThread1] Thread loop at 1
[CustomThread1] Thread loop at 2
[CustomThread] Thread start.
[CustomThread1] Thread loop at 3
[CustomThread1] Thread loop at 4
[CustomThread1] Thread end.
[CustomThread] Thread end.
main end!


public class ThreadJoin2 {      public static void main(String[] args) {        Thread t = new Thread(new RunnableImpl());          new ThreadTest(t).start();          t.start();          try {              t.join(1000);  // join需要调用wait获取t对象的锁,而这个锁已经被上一个线程获取到了,            //所以会阻塞在这里,实际阻塞时间是9000+1000            System.out.println("joinFinish");          } catch (InterruptedException e) {              e.printStackTrace();                 }      }  }    class RunnableImpl implements Runnable {      @Override      public void run() {              try {                  System.out.println("Begin sleep");                  Thread.sleep(1000);                  System.out.println("End sleep");              } catch (InterruptedException e) {                  e.printStackTrace();              }          }  }    class ThreadTest extends Thread {    Thread thread;      public ThreadTest(Thread thread) {        this.thread = thread;      }        @Override      public void run() {          holdThreadLock();      }        public void holdThreadLock() {          //用当前的线程当做lock          synchronized (thread) {              System.out.println("getObjectLock");              try {                  Thread.sleep(9*1000);              } catch (InterruptedException ex) {               ex.printStackTrace();              }              System.out.println("ReleaseObjectLock");          }        }  }  


输出结果为:


getObjectLock
Begin sleep
End sleep
ReleaseObjectLock
joinFinish