为什么await()后会执行lock.unlock,await()时不就释放锁了吗

为什么await()后会执行lock.unlock,await()时不就释放锁了吗

1 是的,释放锁是为了别的线程获得,是为了线程间的通信,是临时释放的,真正满足继续向下执行条件后,被唤醒后获得了锁,做完想做的事后仍需要释放锁,也是为了别的线程能执行或使用共享资源

2 条件锁的理解

Condition c1 = l.newCondition()相当于房间增加了一个门的锁,这个门作为一个交流特殊信息通道与别的线程通信,进行线程间的信息交互,条件锁可以有多把,也就是可以为N种信息与别的线程进行协调交流

举例:

------------------------------------阿姆斯特朗大学梁教授案例讲解

package javajinjie.char29.threadpool;


import java.util.concurrent.*;
import java.util.concurrent.locks.*;


public class ThreadCooperation {
  private static Account account = new Account();
  private  static int  delayTime =  8000;
  
  


  public static void main(String[] args) {
    // Create a thread pool with two threads
    ExecutorService executor = Executors.newFixedThreadPool(2);
    executor.execute(new DepositTask());
    executor.execute(new WithdrawTask());
    executor.shutdown();


    System.out.println("Thread 1\t\tThread 2\t\tBalance");
  }


  public static class DepositTask implements Runnable {
    @Override // Keep adding an amount to the account
    public void run() {
      try { // Purposely delay it to let the withdraw method proceed
        while (true) {
       
        int deposit = (int)(Math.random()*10) ;
       
          account.deposit(deposit);
        //  System.out.println("DepositTask:"  + deposit );
          Thread.sleep(delayTime);
        }
      }
      catch (InterruptedException ex) {
        ex.printStackTrace();
      }
    }
  }


  public static class WithdrawTask implements Runnable {
    @Override // Keep subtracting an amount from the account
    public void run() {
      while (true) {
//        account.withdraw((int)(Math.random() * 10) + 1);
      account.withdraw(10);
      try {
Thread.sleep(delayTime);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
      }
    }
  }


  // An inner class for account
  private static class Account {
    // Create a new lock
    private static Lock lock = new ReentrantLock();


    // Create a condition
    private static Condition newDeposit = lock.newCondition();


    private int balance = 0;


    public int getBalance() {
      return balance;
    }


    public void withdraw(int amount) {
      lock.lock(); // Acquire the lock
      try {
        if (balance < amount) {
          System.out.println("\t\t\t withdraw "+ amount+ " need Wait for a deposit now balance " + getBalance() );
          newDeposit.await();
          System.out.println("\t\t  newDeposit.await() after:---------------------" );
        }
        else
        {      
        balance -= amount; 
        System.out.println("\t\t not await() withdraw  amount:" + amount + "\t\t"+getBalance());
        }
        
      //  System.out.println("now balance >= amount not await(),let's Go");
      }
      catch (InterruptedException ex) {
        ex.printStackTrace();
      }
      finally {
        lock.unlock(); // Release the lock
      }
    }


    public void deposit(int amount) {
      lock.lock(); // Acquire the lock
      try {
        balance += amount;
        System.out.println("Deposit " + amount +
          "\t\t\t\t\t" + getBalance() + "signalAll");
      //  System.out.println("signalAll");
        // Signal thread waiting on the condition
        newDeposit.signalAll();
      }
      finally {
        lock.unlock(); // Release the lock
      }
    }
  }
}


//结果分析
//结论如下
 1  newDeposit.await() after:---------------------   都紧跟在signalAll后执行,说明每次唤醒线程都是从await中走出来
2  个人认为await的外层是if还是while效果是一样的,一般执行await的线程任务一般都是一个while死循环任务


下面为执行结果
withdraw 10 need Wait for a deposit now balance 0
Thread 1 Thread 2 Balance
Deposit 4 4signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 4
Deposit 6 10signalAll
  newDeposit.await() after:---------------------
Deposit 8 18signalAll
not await() withdraw  amount:10 8
withdraw 10 need Wait for a deposit now balance 8
Deposit 5 13signalAll
  newDeposit.await() after:---------------------
Deposit 1 14signalAll
not await() withdraw  amount:10 4
withdraw 10 need Wait for a deposit now balance 4
Deposit 7 11signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 1
Deposit 4 5signalAll
Deposit 7 12signalAll
not await() withdraw  amount:10 2
withdraw 10 need Wait for a deposit now balance 2
Deposit 1 3signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 3
Deposit 8 11signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 1
Deposit 2 3signalAll
withdraw 10 need Wait for a deposit now balance 3
Deposit 9 12signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 2
Deposit 8 10signalAll
not await() withdraw  amount:10 0
Deposit 9 9signalAll
Deposit 0 9signalAll
withdraw 10 need Wait for a deposit now balance 9
Deposit 0 9signalAll
  newDeposit.await() after:---------------------
Deposit 0 9signalAll
withdraw 10 need Wait for a deposit now balance 9
Deposit 1 10signalAll
  newDeposit.await() after:---------------------
Deposit 8 18signalAll
not await() withdraw  amount:10 8
withdraw 10 need Wait for a deposit now balance 8
Deposit 9 17signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 7
Deposit 5 12signalAll
Deposit 0 12signalAll
not await() withdraw  amount:10 2
withdraw 10 need Wait for a deposit now balance 2
Deposit 2 4signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 4
Deposit 1 5signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 5
Deposit 7 12signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 2
Deposit 4 6signalAll
Deposit 9 15signalAll
not await() withdraw  amount:10 5
withdraw 10 need Wait for a deposit now balance 5
Deposit 1 6signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 6
Deposit 6 12signalAll
  newDeposit.await() after:---------------------
Deposit 0 12signalAll
not await() withdraw  amount:10 2
withdraw 10 need Wait for a deposit now balance 2
Deposit 4 6signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 6
Deposit 3 9signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 9
Deposit 3 12signalAll
  newDeposit.await() after:---------------------
Deposit 4 16signalAll
not await() withdraw  amount:10 6
withdraw 10 need Wait for a deposit now balance 6
Deposit 7 13signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 3
Deposit 3 6signalAll
withdraw 10 need Wait for a deposit now balance 6
Deposit 5 11signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 1
Deposit 7 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 2 10signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 0
Deposit 4 4signalAll
withdraw 10 need Wait for a deposit now balance 4
Deposit 9 13signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 3
Deposit 3 6signalAll
Deposit 4 10signalAll
not await() withdraw  amount:10 0
withdraw 10 need Wait for a deposit now balance 0
Deposit 3 3signalAll
  newDeposit.await() after:---------------------
Deposit 5 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 5 13signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 3
Deposit 4 7signalAll
Deposit 0 7signalAll
withdraw 10 need Wait for a deposit now balance 7
Deposit 4 11signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 1
Deposit 3 4signalAll
Deposit 1 5signalAll
withdraw 10 need Wait for a deposit now balance 5
Deposit 9 14signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 4
Deposit 2 6signalAll
withdraw 10 need Wait for a deposit now balance 6
Deposit 8 14signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 4
Deposit 2 6signalAll
withdraw 10 need Wait for a deposit now balance 6
Deposit 7 13signalAll
  newDeposit.await() after:---------------------
Deposit 2 15signalAll
not await() withdraw  amount:10 5
withdraw 10 need Wait for a deposit now balance 5
Deposit 6 11signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 1
Deposit 4 5signalAll
Deposit 4 9signalAll
withdraw 10 need Wait for a deposit now balance 9
Deposit 0 9signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 9
Deposit 4 13signalAll
  newDeposit.await() after:---------------------
Deposit 0 13signalAll
not await() withdraw  amount:10 3
withdraw 10 need Wait for a deposit now balance 3
Deposit 6 9signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 9
Deposit 0 9signalAll
  newDeposit.await() after:---------------------
Deposit 8 17signalAll
not await() withdraw  amount:10 7
Deposit 2 9signalAll
withdraw 10 need Wait for a deposit now balance 9
Deposit 0 9signalAll
  newDeposit.await() after:---------------------
Deposit 7 16signalAll
not await() withdraw  amount:10 6
withdraw 10 need Wait for a deposit now balance 6
Deposit 7 13signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 3
Deposit 1 4signalAll
withdraw 10 need Wait for a deposit now balance 4
Deposit 6 10signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 0
Deposit 5 5signalAll
withdraw 10 need Wait for a deposit now balance 5
Deposit 3 8signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 8
Deposit 0 8signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 8
Deposit 5 13signalAll
  newDeposit.await() after:---------------------
Deposit 5 18signalAll
not await() withdraw  amount:10 8
withdraw 10 need Wait for a deposit now balance 8
Deposit 5 13signalAll
  newDeposit.await() after:---------------------
Deposit 4 17signalAll
not await() withdraw  amount:10 7
withdraw 10 need Wait for a deposit now balance 7
Deposit 7 14signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 4
Deposit 4 8signalAll
Deposit 9 17signalAll
not await() withdraw  amount:10 7
withdraw 10 need Wait for a deposit now balance 7
Deposit 3 10signalAll
  newDeposit.await() after:---------------------
Deposit 5 15signalAll
not await() withdraw  amount:10 5
Deposit 8 13signalAll
not await() withdraw  amount:10 3
Deposit 1 4signalAll
withdraw 10 need Wait for a deposit now balance 4
Deposit 9 13signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 3
Deposit 4 7signalAll
withdraw 10 need Wait for a deposit now balance 7
Deposit 9 16signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 6
Deposit 0 6signalAll
withdraw 10 need Wait for a deposit now balance 6
Deposit 0 6signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 6
Deposit 1 7signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 7
Deposit 0 7signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 7
Deposit 4 11signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 1
Deposit 7 8signalAll
Deposit 9 17signalAll
not await() withdraw  amount:10 7
withdraw 10 need Wait for a deposit now balance 7
Deposit 4 11signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 1
Deposit 5 6signalAll
withdraw 10 need Wait for a deposit now balance 6
Deposit 2 8signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 8
Deposit 7 15signalAll
  newDeposit.await() after:---------------------
Deposit 2 17signalAll
not await() withdraw  amount:10 7
Deposit 3 10signalAll
not await() withdraw  amount:10 0
withdraw 10 need Wait for a deposit now balance 0
Deposit 0 0signalAll
  newDeposit.await() after:---------------------
Deposit 4 4signalAll
withdraw 10 need Wait for a deposit now balance 4
Deposit 6 10signalAll
  newDeposit.await() after:---------------------
Deposit 8 18signalAll
not await() withdraw  amount:10 8
withdraw 10 need Wait for a deposit now balance 8
Deposit 8 16signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 6
Deposit 0 6signalAll
Deposit 5 11signalAll
not await() withdraw  amount:10 1
Deposit 0 1signalAll
withdraw 10 need Wait for a deposit now balance 1
Deposit 7 8signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 8
Deposit 0 8signalAll
  newDeposit.await() after:---------------------
Deposit 9 17signalAll
not await() withdraw  amount:10 7
Deposit 5 12signalAll
not await() withdraw  amount:10 2
Deposit 6 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 0 8signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 8
Deposit 9 17signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 7
Deposit 1 8signalAll
Deposit 3 11signalAll
not await() withdraw  amount:10 1
withdraw 10 need Wait for a deposit now balance 1
Deposit 2 3signalAll
  newDeposit.await() after:---------------------
Deposit 9 12signalAll
not await() withdraw  amount:10 2
withdraw 10 need Wait for a deposit now balance 2
Deposit 0 2signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 2
Deposit 3 5signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 5
Deposit 3 8signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 8
Deposit 2 10signalAll
  newDeposit.await() after:---------------------
Deposit 4 14signalAll
not await() withdraw  amount:10 4
withdraw 10 need Wait for a deposit now balance 4
Deposit 6 10signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 0
Deposit 4 4signalAll
Deposit 1 5signalAll
withdraw 10 need Wait for a deposit now balance 5
Deposit 3 8signalAll
  newDeposit.await() after:---------------------
Deposit 7 15signalAll
not await() withdraw  amount:10 5
withdraw 10 need Wait for a deposit now balance 5
Deposit 6 11signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 1
Deposit 9 10signalAll
not await() withdraw  amount:10 0
Deposit 7 7signalAll
Deposit 6 13signalAll
not await() withdraw  amount:10 3
Deposit 9 12signalAll
not await() withdraw  amount:10 2
Deposit 1 3signalAll
withdraw 10 need Wait for a deposit now balance 3
Deposit 6 9signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 9
Deposit 9 18signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 8
Deposit 3 11signalAll
not await() withdraw  amount:10 1
Deposit 1 2signalAll
withdraw 10 need Wait for a deposit now balance 2
Deposit 2 4signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 4
Deposit 9 13signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 3
Deposit 5 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 0 8signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 8
Deposit 2 10signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 0
Deposit 3 3signalAll
Deposit 1 4signalAll
withdraw 10 need Wait for a deposit now balance 4
Deposit 8 12signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 2
Deposit 1 3signalAll
withdraw 10 need Wait for a deposit now balance 3
Deposit 7 10signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 0
Deposit 2 2signalAll
withdraw 10 need Wait for a deposit now balance 2
Deposit 9 11signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 1
Deposit 4 5signalAll
Deposit 7 12signalAll
not await() withdraw  amount:10 2
withdraw 10 need Wait for a deposit now balance 2
Deposit 6 8signalAll
  newDeposit.await() after:---------------------
Deposit 5 13signalAll
not await() withdraw  amount:10 3
withdraw 10 need Wait for a deposit now balance 3
Deposit 7 10signalAll
  newDeposit.await() after:---------------------
Deposit 0 10signalAll
not await() withdraw  amount:10 0
withdraw 10 need Wait for a deposit now balance 0
Deposit 2 2signalAll
  newDeposit.await() after:---------------------
Deposit 0 2signalAll
withdraw 10 need Wait for a deposit now balance 2
Deposit 4 6signalAll
  newDeposit.await() after:---------------------
Deposit 3 9signalAll
withdraw 10 need Wait for a deposit now balance 9
Deposit 6 15signalAll
  newDeposit.await() after:---------------------
Deposit 1 16signalAll
not await() withdraw  amount:10 6
Deposit 9 15signalAll
not await() withdraw  amount:10 5
Deposit 4 9signalAll
withdraw 10 need Wait for a deposit now balance 9
Deposit 5 14signalAll
  newDeposit.await() after:---------------------
Deposit 7 21signalAll
not await() withdraw  amount:10 11
not await() withdraw  amount:10 1
Deposit 9 10signalAll
Deposit 2 12signalAll
not await() withdraw  amount:10 2
Deposit 3 5signalAll
withdraw 10 need Wait for a deposit now balance 5
Deposit 8 13signalAll
  newDeposit.await() after:---------------------
Deposit 7 20signalAll
not await() withdraw  amount:10 10
not await() withdraw  amount:10 0
Deposit 6 6signalAll
Deposit 0 6signalAll
withdraw 10 need Wait for a deposit now balance 6
Deposit 6 12signalAll
  newDeposit.await() after:---------------------
Deposit 3 15signalAll
not await() withdraw  amount:10 5
withdraw 10 need Wait for a deposit now balance 5
Deposit 6 11signalAll
  newDeposit.await() after:---------------------
Deposit 9 20signalAll
not await() withdraw  amount:10 10
not await() withdraw  amount:10 0
Deposit 5 5signalAll
withdraw 10 need Wait for a deposit now balance 5
Deposit 7 12signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 2
Deposit 6 8signalAll
Deposit 3 11signalAll
not await() withdraw  amount:10 1
Deposit 6 7signalAll
withdraw 10 need Wait for a deposit now balance 7
Deposit 1 8signalAll
  newDeposit.await() after:---------------------
Deposit 6 14signalAll
not await() withdraw  amount:10 4
Deposit 0 4signalAll
withdraw 10 need Wait for a deposit now balance 4
Deposit 2 6signalAll
  newDeposit.await() after:---------------------
Deposit 3 9signalAll
withdraw 10 need Wait for a deposit now balance 9
Deposit 9 18signalAll
  newDeposit.await() after:---------------------
Deposit 1 19signalAll
not await() withdraw  amount:10 9
withdraw 10 need Wait for a deposit now balance 9
Deposit 4 13signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 3
Deposit 2 5signalAll
Deposit 7 12signalAll
not await() withdraw  amount:10 2
withdraw 10 need Wait for a deposit now balance 2
Deposit 0 2signalAll
  newDeposit.await() after:---------------------
Deposit 6 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 4 12signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 2
Deposit 5 7signalAll
Deposit 2 9signalAll
withdraw 10 need Wait for a deposit now balance 9
Deposit 5 14signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 4
Deposit 9 13signalAll
not await() withdraw  amount:10 3
Deposit 5 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 0 8signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 8
Deposit 4 12signalAll
  newDeposit.await() after:---------------------
Deposit 0 12signalAll
not await() withdraw  amount:10 2
withdraw 10 need Wait for a deposit now balance 2
Deposit 7 9signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 9
Deposit 8 17signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 7
Deposit 4 11signalAll
Deposit 3 14signalAll
not await() withdraw  amount:10 4
withdraw 10 need Wait for a deposit now balance 4
Deposit 0 4signalAll
  newDeposit.await() after:---------------------
Deposit 2 6signalAll
withdraw 10 need Wait for a deposit now balance 6
Deposit 2 8signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 8
Deposit 3 11signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 1
Deposit 4 5signalAll
Deposit 6 11signalAll
not await() withdraw  amount:10 1
withdraw 10 need Wait for a deposit now balance 1
Deposit 1 2signalAll
  newDeposit.await() after:---------------------
Deposit 4 6signalAll
withdraw 10 need Wait for a deposit now balance 6
Deposit 8 14signalAll
  newDeposit.await() after:---------------------
Deposit 3 17signalAll
not await() withdraw  amount:10 7
withdraw 10 need Wait for a deposit now balance 7
Deposit 1 8signalAll
  newDeposit.await() after:---------------------
Deposit 7 15signalAll
not await() withdraw  amount:10 5
Deposit 2 7signalAll
withdraw 10 need Wait for a deposit now balance 7
Deposit 6 13signalAll
  newDeposit.await() after:---------------------
Deposit 8 21signalAll
not await() withdraw  amount:10 11
Deposit 9 20signalAll
not await() withdraw  amount:10 10
not await() withdraw  amount:10 0
Deposit 6 6signalAll
Deposit 4 10signalAll
not await() withdraw  amount:10 0
withdraw 10 need Wait for a deposit now balance 0
Deposit 9 9signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 9
Deposit 6 15signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 5
Deposit 8 13signalAll
Deposit 6 19signalAll
not await() withdraw  amount:10 9
withdraw 10 need Wait for a deposit now balance 9
Deposit 3 12signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 2
Deposit 9 11signalAll
Deposit 7 18signalAll
not await() withdraw  amount:10 8
withdraw 10 need Wait for a deposit now balance 8
Deposit 3 11signalAll
  newDeposit.await() after:---------------------
Deposit 7 18signalAll
not await() withdraw  amount:10 8
withdraw 10 need Wait for a deposit now balance 8
Deposit 4 12signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 2
Deposit 9 11signalAll
not await() withdraw  amount:10 1
Deposit 1 2signalAll
Deposit 6 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 4 12signalAll
  newDeposit.await() after:---------------------
Deposit 1 13signalAll
not await() withdraw  amount:10 3
Deposit 3 6signalAll
withdraw 10 need Wait for a deposit now balance 6
Deposit 1 7signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 7
Deposit 2 9signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 9
Deposit 1 10signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 0
Deposit 6 6signalAll
withdraw 10 need Wait for a deposit now balance 6
Deposit 0 6signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 6
Deposit 8 14signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 4
Deposit 9 13signalAll
Deposit 8 21signalAll
not await() withdraw  amount:10 11
not await() withdraw  amount:10 1
Deposit 9 10signalAll
not await() withdraw  amount:10 0
Deposit 3 3signalAll
Deposit 5 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 3 11signalAll
  newDeposit.await() after:---------------------
Deposit 1 12signalAll
not await() withdraw  amount:10 2
Deposit 0 2signalAll
withdraw 10 need Wait for a deposit now balance 2
Deposit 9 11signalAll
  newDeposit.await() after:---------------------
Deposit 8 19signalAll
not await() withdraw  amount:10 9
Deposit 1 10signalAll
not await() withdraw  amount:10 0
Deposit 1 1signalAll
withdraw 10 need Wait for a deposit now balance 1
Deposit 9 10signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 0
Deposit 6 6signalAll
withdraw 10 need Wait for a deposit now balance 6
Deposit 7 13signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 3
Deposit 7 10signalAll
not await() withdraw  amount:10 0
Deposit 8 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 0 8signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 8
Deposit 3 11signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 1
Deposit 8 9signalAll
withdraw 10 need Wait for a deposit now balance 9
Deposit 3 12signalAll
  newDeposit.await() after:---------------------
Deposit 2 14signalAll
not await() withdraw  amount:10 4
Deposit 9 13signalAll
not await() withdraw  amount:10 3
withdraw 10 need Wait for a deposit now balance 3
Deposit 5 8signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 8
Deposit 6 14signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 4
Deposit 8 12signalAll
Deposit 8 20signalAll
not await() withdraw  amount:10 10
not await() withdraw  amount:10 0
Deposit 4 4signalAll
Deposit 4 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 8 16signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 6
Deposit 5 11signalAll
not await() withdraw  amount:10 1
Deposit 1 2signalAll
Deposit 2 4signalAll
withdraw 10 need Wait for a deposit now balance 4
Deposit 3 7signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 7
Deposit 8 15signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 5
Deposit 9 14signalAll
not await() withdraw  amount:10 4
Deposit 8 12signalAll
Deposit 1 13signalAll
not await() withdraw  amount:10 3
withdraw 10 need Wait for a deposit now balance 3
Deposit 1 4signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 4
Deposit 5 9signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 9
Deposit 9 18signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 8
Deposit 2 10signalAll
not await() withdraw  amount:10 0
Deposit 7 7signalAll
Deposit 5 12signalAll
not await() withdraw  amount:10 2
withdraw 10 need Wait for a deposit now balance 2
Deposit 5 7signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 7
Deposit 8 15signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 5
Deposit 1 6signalAll
Deposit 6 12signalAll
not await() withdraw  amount:10 2
withdraw 10 need Wait for a deposit now balance 2
Deposit 9 11signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 1
Deposit 2 3signalAll
withdraw 10 need Wait for a deposit now balance 3
Deposit 5 8signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 8
Deposit 3 11signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 1
Deposit 7 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 4 12signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 2
Deposit 6 8signalAll
Deposit 1 9signalAll
withdraw 10 need Wait for a deposit now balance 9
Deposit 4 13signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 3
Deposit 1 4signalAll
Deposit 3 7signalAll
withdraw 10 need Wait for a deposit now balance 7
Deposit 1 8signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 8
Deposit 4 12signalAll
  newDeposit.await() after:---------------------
Deposit 3 15signalAll
not await() withdraw  amount:10 5
withdraw 10 need Wait for a deposit now balance 5
Deposit 3 8signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 8
Deposit 9 17signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 7
Deposit 8 15signalAll
not await() withdraw  amount:10 5
Deposit 7 12signalAll
Deposit 7 19signalAll
not await() withdraw  amount:10 9
withdraw 10 need Wait for a deposit now balance 9
Deposit 2 11signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 1
Deposit 7 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 0 8signalAll
  newDeposit.await() after:---------------------
Deposit 4 12signalAll
not await() withdraw  amount:10 2
withdraw 10 need Wait for a deposit now balance 2
Deposit 8 10signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 0
Deposit 5 5signalAll
Deposit 1 6signalAll
withdraw 10 need Wait for a deposit now balance 6
Deposit 2 8signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 8
Deposit 3 11signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 1
Deposit 5 6signalAll
Deposit 7 13signalAll
not await() withdraw  amount:10 3
withdraw 10 need Wait for a deposit now balance 3
Deposit 8 11signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 1
Deposit 6 7signalAll
withdraw 10 need Wait for a deposit now balance 7
Deposit 0 7signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 7
Deposit 1 8signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 8
Deposit 1 9signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 9
Deposit 2 11signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 1
Deposit 2 3signalAll
withdraw 10 need Wait for a deposit now balance 3
Deposit 0 3signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 3
Deposit 8 11signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 1
Deposit 7 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 8 16signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 6
Deposit 5 11signalAll
not await() withdraw  amount:10 1
Deposit 7 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 3 11signalAll
  newDeposit.await() after:---------------------
Deposit 9 20signalAll
not await() withdraw  amount:10 10
Deposit 5 15signalAll
not await() withdraw  amount:10 5
Deposit 3 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 5 13signalAll
  newDeposit.await() after:---------------------
Deposit 9 22signalAll
not await() withdraw  amount:10 12
Deposit 1 13signalAll
not await() withdraw  amount:10 3
Deposit 7 10signalAll
not await() withdraw  amount:10 0
Deposit 1 1signalAll
withdraw 10 need Wait for a deposit now balance 1
Deposit 5 6signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 6
Deposit 7 13signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 3
Deposit 3 6signalAll
withdraw 10 need Wait for a deposit now balance 6
Deposit 3 9signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 9
Deposit 5 14signalAll
  newDeposit.await() after:---------------------
Deposit 6 20signalAll
not await() withdraw  amount:10 10
Deposit 4 14signalAll
not await() withdraw  amount:10 4
withdraw 10 need Wait for a deposit now balance 4
Deposit 1 5signalAll
  newDeposit.await() after:---------------------
Deposit 3 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 9 17signalAll
  newDeposit.await() after:---------------------
Deposit 9 26signalAll
not await() withdraw  amount:10 16
Deposit 3 19signalAll
not await() withdraw  amount:10 9
Deposit 1 10signalAll
not await() withdraw  amount:10 0
Deposit 1 1signalAll
withdraw 10 need Wait for a deposit now balance 1
Deposit 4 5signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 5
Deposit 4 9signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 9
Deposit 5 14signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 4
Deposit 0 4signalAll
withdraw 10 need Wait for a deposit now balance 4
Deposit 2 6signalAll
  newDeposit.await() after:---------------------
Deposit 2 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 9 17signalAll
  newDeposit.await() after:---------------------
Deposit 3 20signalAll
not await() withdraw  amount:10 10
Deposit 2 12signalAll
not await() withdraw  amount:10 2
Deposit 5 7signalAll
withdraw 10 need Wait for a deposit now balance 7
Deposit 3 10signalAll
  newDeposit.await() after:---------------------
Deposit 7 17signalAll
not await() withdraw  amount:10 7
withdraw 10 need Wait for a deposit now balance 7
Deposit 1 8signalAll
  newDeposit.await() after:---------------------
Deposit 2 10signalAll
not await() withdraw  amount:10 0
withdraw 10 need Wait for a deposit now balance 0
Deposit 4 4signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 4
Deposit 8 12signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 2
Deposit 0 2signalAll
withdraw 10 need Wait for a deposit now balance 2
Deposit 7 9signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 9
Deposit 6 15signalAll
  newDeposit.await() after:---------------------
Deposit 3 18signalAll
not await() withdraw  amount:10 8
withdraw 10 need Wait for a deposit now balance 8
Deposit 1 9signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 9
Deposit 9 18signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 8
Deposit 9 17signalAll
Deposit 6 23signalAll
not await() withdraw  amount:10 13
not await() withdraw  amount:10 3
Deposit 5 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 0 8signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 8
Deposit 4 12signalAll
  newDeposit.await() after:---------------------
Deposit 6 18signalAll
not await() withdraw  amount:10 8
withdraw 10 need Wait for a deposit now balance 8
Deposit 0 8signalAll
  newDeposit.await() after:---------------------
withdraw 10 need Wait for a deposit now balance 8
Deposit 9 17signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 7
Deposit 3 10signalAll
not await() withdraw  amount:10 0
Deposit 8 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 8 16signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 6
Deposit 7 13signalAll
not await() withdraw  amount:10 3
Deposit 2 5signalAll
withdraw 10 need Wait for a deposit now balance 5
Deposit 9 14signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 4
Deposit 2 6signalAll
Deposit 9 15signalAll
not await() withdraw  amount:10 5
withdraw 10 need Wait for a deposit now balance 5
Deposit 7 12signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 2
Deposit 8 10signalAll
not await() withdraw  amount:10 0
Deposit 8 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 9 17signalAll
  newDeposit.await() after:---------------------
not await() withdraw  amount:10 7
Deposit 1 8signalAll
withdraw 10 need Wait for a deposit now balance 8
Deposit 4 12signalAll
  newDeposit.await() after:---------------------






Java Lock类 await()和signal() 我知道你在疑惑什么

  (2013-10-19 20:50:38)
标签: 

java

 

等待与唤醒

 

lock

 

await和signal

 

it

分类: Java学习笔记
比起synchronized那一套,Lock这一套更形象,Lock可以完全代替synchronized那一套。

与synchronized唯一不同的是,synchronized只能拥有一把锁,而Lock可以同时有很多把锁。

最基本的,你需要引入两个类:
java.util.concurrent.locks.Lock;
java.util.concurrent.locks.ReentrantLock;

然后实例化Lock:
Lock l = new ReentrantLock();

现在就可以用l制作同步代码块。
看小程序:
Java <wbr>Lock类 <wbr>await()和signal() <wbr>我知道你在疑惑什么


------------
l.lock()和l.unlock()完全代替了synchronized。
在l.lock()和l.unlock()之间的代码就是专属于l的同步代码块。与synchronized功能完全一样。
在同一时刻,只能有一个线程执行其中的代码。

【await()和signal()】

你需要导入另外一个类:
java.util.concurrent.locks.Condition;

以前我们用synchronized时,总是给它指定一个锁,比如:synchronized("我是锁"){};
这就是一个以字符串对象"我是锁"为锁的同步代码块。
在lock中,要使用等待和唤醒,也需要指定这样锁,必须用到Condition类。

在l上绑定锁:Condition c1 = l.newCondition(); 这样在l上就绑定了一把小锁。
与synchronized不同的是,l可以绑定任意多把锁:Condition c2 = l.newCondition();

看小程序:
Java <wbr>Lock类 <wbr>await()和signal() <wbr>我知道你在疑惑什么

------------
对以上程序的解释:
定义了一个Info类,其中有两个方法Run1和Run2,都是输出10次XXX正在运行。
Info中的程序都是静态的,方便其他类调用。

以上程序的流程是:
a线程(new Thread(a)这个线程)执行后,
      发现l没有被占用,于是将其占有。
      输出一句话
      然后执行c2.signal();因为没有线程在c2上等待,所以此时执行这一句没有任何效果。
      接着执行了c1.await(); 导致当前线程(就是a线程)停止,并解除占用l。a线程在c1上等待。

于此同时,b线程(new Thread(b))这个线程也在执行。
      b发现l没有被占用,将其占用。
      输出一句话
      然后执行c1.signal();我们知道在c1上有个线程正在等待,就是a线程,现在将其唤醒。a线程开始抢占l,但l此时是被b占有着的。
      b线程接着执行了c2.await();导致当前线程(b线程)停止,并解除占用l,b线程在c2上等待。
      此时a线程占有了l,如此循环...
------------

总结:1、l.lock()和l.unlock()完全代替synchronized()
         2、await()、signal()、signalAll()与wait()、notify()、nitofyAll()功能完全相同。
         3、l可以有多把锁。不同的锁专门用于锁不同的线程,比较明了,不像synchronized,只能用一把锁。
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值