为什么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,只能用一把锁。
查看全文
如若内容造成侵权/违法违规/事实不符,请联系编程学习网邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

相关文章

  1. 详解:Anaconda配置多版本Python+Pycharm更换Python版本+新版本第三方包的使用

    1 Anaconda配置多版本Python 查看当前python版本,为Python3.7.4,如果想要在Anaconda中配置多版本Python,具体过程如下所示:1、首先为即将创建的Python版本命名一个名字,以下安装Python3.6.4,故这里命名为py364。 2、使用以下命令创建新环境:conda create -n env_name li…...

    2024/4/24 16:23:10
  2. CentOS下安装neo4j

    2019独角兽企业重金招聘Python工程师标准>>> 本文记录一下在CentOS 6.7上,安装neo4j图数据库,本文安装的版本为neo4j-community-2.3.9-unix.tar.gz。 下载Neo4j安装包 使用wget命令获取Neo4j安装包~如: [root@dev18 srv]# wget https://neo4j.com/artifact.php…...

    2024/5/1 23:03:39
  3. 无返回值的异步方法能否不用await

    1、无返回值的异步方法能否不用await?如果你不需要等待加一的操作完成,那就可以直接执行后面的操作。那要看你的需求了,如果你后面的操作必须在加一的操作后执行,那就要await了2、请问C#中如何判断无返回值的await 方法是否执行成功? 我在调用.net自带的方法发送websocket…...

    2024/4/14 21:39:41
  4. Netty之JavaBIO编程模型介绍

    最近打算再次整理下Netty的相关内容,但是要把Netty弄的比较清楚,我们首先需要对Java中的BIO,NIO及AIO要比较清楚,所以我们前面会花几篇文章先把这块的内容整理出来。 JavaBIO编程模型介绍 1.I/O模型介绍 1.1 什么是I/O模型简单的理解:就是用什么样的通道进行数据的发送和接…...

    2024/4/14 21:39:39
  5. BIGEMAP使用Bigemap下载地图生成GST(Mapinfo格式)地图包

    使用Bigemap下载地图生成GST(Mapinfo格式)地图包发布时间:2019-01-11 版权:使用的软件:1、 Bigemap地图下载器2、 Global Mapper 173、 MapInfo Professional 12.0第一步: 用Bigemap地图下载器下载地图和矢量数据:1、1 打开bigemap地图下载器选择目标区域:四川省成都市…...

    2024/5/1 22:40:06
  6. C语言循环移位-C语言范例宝典50

    循环移位与算术移位的区别已经介绍的很详细了,程序中遇到两个问题,一个是scanf与scanf_s的区别,貌似是VS为防止溢出,参见: 修改scanf设置还有注意便是int类型所占的字节数在Turbo C和VS编辑器是不一样的。 也跟使用系统的位数有关X8or,X64; 具体占用,可用sizeof()命令…...

    2024/4/19 1:26:49
  7. MapInfo简介(转)

    MapInfo是一个开发桌面地图软件及GIS分析系统的公司。其旗下的多种软件产品都是用来支持GIS的二次开发的。刚刚接触MapInfo的开发者一般都会被这些产品的关系弄的晕头转向。MapInfo Professional是一套基于Windows平台的地图化信息解决方案。可以方便、直观的展现数据和地理信息…...

    2024/4/19 8:18:54
  8. centos常用命令记录

    以下内容centos8 测试使用替换阿里镜像1)备份mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup2)更新镜像(根据系统版本换地址)wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo1.查看yum 安装某…...

    2024/5/2 0:03:52
  9. java bio,nio,aio及源码

    NIO简介随着JavaIO类库的不断发展和改进,基于Java的网络编程会变得越来越简单。随着异步IO功能的增强,基于JavaNIO开发的网络服务器甚至不逊色与C++开发的网络程序。 记录一下学习BIO、NIO编程模型以及JDK1.7提供的NIO2.0的使用。传统的BIO编程这个可以搜索一下socket,就有很…...

    2024/4/14 21:39:35
  10. 通过移位运算(左移)实现2的n次方的计算

    输入描述:多组输入,每一行输入整数n(0 <= n < 31)。输出描述:针对每组输入输出对应的2的n次方的结果。示例1输入2 10输出4 1024#include <bits/stdc++.h> using namespace std;int main() {int n;while(scanf("%d", &n) == 1) {printf("%d\n…...

    2024/5/1 22:04:19
  11. 宏任务 && 微任务 && async && await(图解)

    1.宏任务和微任务宏任务和微任务是等待任务队列中的异步任务的处理机制;(JS执行有同步任务队列和等待任务队列)浏览器的任务队列:主任务队列存储的都是同步任务;等待任务队列存储的都是异步任务;首先浏览器会把主任务队列中的同步任务挨个全部执行完,然后再去等待任务队列…...

    2024/4/24 16:23:01
  12. ubuntu 16.04 安装neo4j

    可以在官网上下载ubuntu版本进行手动安装,不过下载的速度有点慢,这里使用ubuntu apt命令进行安装,请确保ubuntu上有jdk,如果没有,请自行百度安装一下jdk,用apt命令安装有一个好处是,所有的配置都自动做了,比如环境变量:wget -O - https://debian.neo4j.org/neotechnol…...

    2024/4/24 16:23:03
  13. neo4j安装及学习

    图形数据库(Graph Database)是NoSQL数据库家族中特殊的存在,用于存储丰富的关系数据。Neo4j是一个高性能的NOSQL图形数据库,也是目前最流行的图形数据库,支持完整的事务,它将结构化数据存储在网络上而不是表中。在属性图中,图是由顶点(Vertex),边(Edge)和属性(Pro…...

    2024/4/24 16:23:02
  14. 探讨一下C语言中char类型数组的移位操作

    char a[4] = {1, 2, 3, 4}; unsigned int b; b = a[0]<<24 + a[1]<<16 + a[2]<<8 + a[3]; b的结果是多少? 实际编程是0,与想的有些差距,不知道这种操作实际上如何处理?...

    2024/4/24 16:22:58
  15. 谈谈我对java的BIO和NIO的学习的理解

    首先io是人机交互的前提 是非常重要滴java在早期只有bio 后面更新出来了nio nio的作用越来越重要有的人称nio为阻塞式io 这点我觉得很不严谨 而且对于阻塞与非阻塞的概念我看很多人的说法也不一致在此我只说说我自己的认识 毕竟认知也是一个不断提升和完善的过程 就像地球由方到…...

    2024/4/24 16:23:04
  16. JavaSE 常用类(JDK8中的日期和时间的API)

    JDK8中的日期和时间的API 一.jdk8之前的API存在着一些问题: ①可变性:像日期和时间这样的类应该是不可变的 ②偏移量:Date中的年份是从1990开始的,而月份都是从0开始的 ③格式化,格式化只对Date有用,Calendar则不行 二.localDate、localTime、localDateTime方法: 1.now(…...

    2024/4/23 18:02:41
  17. 线程进入休眠状态的三种方式:Thread.sleep、Object.wait、LockSupport.park

    一、线程睡眠Thread.sleep1)需要指定睡眠时间,如Thread.sleep(10_000);// 睡眠10秒 // TimeUnit.MINUTES.sleep(1);// 睡眠一分钟2)睡眠时线程状态为TIMED_WAITING(限期等待)。3)需要捕获InterruptedException异常。4)不会释放持有的锁。二、线程等待Object.wait1)可以…...

    2024/4/24 16:22:55
  18. C语言数据类型转换详解

    3. 类型转换 如果有人问C语法规则中最复杂的是哪一部分,我一定会说是类型转换。从上面两节可以看出,有符号、无符号整数和浮点数加起来有那么多种类型,每两种类型之间都要定义一个转换规则,转换规则的数量自然很庞大,更何况由于各种体系结构对于整数和浮点数的实现很不相同…...

    2024/4/24 16:22:55
  19. yum快速安装neo4j

    本篇教程为快速使用yum安装neo4j 可能大家体验过yum安装neo4j非常缓慢的经历,在这里给大家提供一个非常有效的解决办法添加官方keycd /tmp wget http://debian.neo4j.org/neotechnology.gpg.key rpm --import neotechnology.gpg.key将官方的yum 库添加到/etc/yum.repos.d/neo4…...

    2024/4/24 16:22:56
  20. 深入浅出JAVA BIO、NIO和AIO(附详细代码实例)

    ==> 学习汇总(持续更新) ==> 从零搭建后端基础设施系列(一)-- 背景介绍1.深入浅出之BIO1.1 简单代码示例1.2 BIO相关概念1.3 一张图理解BIO1.4 Q&A 2.深入浅出之NIO2.1 简单代码示例2.2 NIO相关概念2.3 一张图理解NIO2.4 Q&A 3.深入浅出之AIO3.1 简单代码示…...

    2024/4/28 21:38:56

最新文章

  1. 【Linux 系统】进程信号 -- 详解

    ⚪前言 注意&#xff1a;进程间通信中的信号量跟下面要讲的信号没有任何关系。 一、从不同角度理解信号 1、生活角度的信号 你在网上买了很多件商品&#xff0c;在等待不同商品快递的到来。但即便快递没有到来&#xff0c;你也知道快递来临时&#xff0c;你该怎么处理快递&a…...

    2024/5/2 0:44:21
  2. 梯度消失和梯度爆炸的一些处理方法

    在这里是记录一下梯度消失或梯度爆炸的一些处理技巧。全当学习总结了如有错误还请留言&#xff0c;在此感激不尽。 权重和梯度的更新公式如下&#xff1a; w w − η ⋅ ∇ w w w - \eta \cdot \nabla w ww−η⋅∇w 个人通俗的理解梯度消失就是网络模型在反向求导的时候出…...

    2024/3/20 10:50:27
  3. DDIM,多样性与运行效率之间的trade off

    DDPM的重大缺陷在于其在反向扩散的过程中需要逐步从 x t x_t xt​倒推到 x 0 x_0 x0​&#xff0c;因此其推理速度非常缓慢。相反&#xff0c;DDPM的训练过程是很快的&#xff0c;可以直接根据 x 0 x_0 x0​到 x t x_t xt​添加的高斯噪声 ϵ \epsilon ϵ完成一次训练。 为了解…...

    2024/5/1 13:28:25
  4. 基于物联网的智能家居远程视频监控系统设计与实现

    基于物联网的智能家居远程视频监控系统设计与实现 摘要&#xff1a;随着物联网技术的快速发展&#xff0c;智能家居系统已成为提升家居安全性和便利性的重要手段。本文设计并实现了一套基于物联网的智能家居远程视频监控系统&#xff0c;该系统结合了嵌入式技术、网络通信技术…...

    2024/5/1 3:26:37
  5. 【外汇早评】美通胀数据走低,美元调整

    原标题:【外汇早评】美通胀数据走低,美元调整昨日美国方面公布了新一期的核心PCE物价指数数据,同比增长1.6%,低于前值和预期值的1.7%,距离美联储的通胀目标2%继续走低,通胀压力较低,且此前美国一季度GDP初值中的消费部分下滑明显,因此市场对美联储后续更可能降息的政策…...

    2024/5/1 17:30:59
  6. 【原油贵金属周评】原油多头拥挤,价格调整

    原标题:【原油贵金属周评】原油多头拥挤,价格调整本周国际劳动节,我们喜迎四天假期,但是整个金融市场确实流动性充沛,大事频发,各个商品波动剧烈。美国方面,在本周四凌晨公布5月份的利率决议和新闻发布会,维持联邦基金利率在2.25%-2.50%不变,符合市场预期。同时美联储…...

    2024/4/30 18:14:14
  7. 【外汇周评】靓丽非农不及疲软通胀影响

    原标题:【外汇周评】靓丽非农不及疲软通胀影响在刚结束的周五,美国方面公布了新一期的非农就业数据,大幅好于前值和预期,新增就业重新回到20万以上。具体数据: 美国4月非农就业人口变动 26.3万人,预期 19万人,前值 19.6万人。 美国4月失业率 3.6%,预期 3.8%,前值 3…...

    2024/4/29 2:29:43
  8. 【原油贵金属早评】库存继续增加,油价收跌

    原标题:【原油贵金属早评】库存继续增加,油价收跌周三清晨公布美国当周API原油库存数据,上周原油库存增加281万桶至4.692亿桶,增幅超过预期的74.4万桶。且有消息人士称,沙特阿美据悉将于6月向亚洲炼油厂额外出售更多原油,印度炼油商预计将每日获得至多20万桶的额外原油供…...

    2024/4/30 18:21:48
  9. 【外汇早评】日本央行会议纪要不改日元强势

    原标题:【外汇早评】日本央行会议纪要不改日元强势近两日日元大幅走强与近期市场风险情绪上升,避险资金回流日元有关,也与前一段时间的美日贸易谈判给日本缓冲期,日本方面对汇率问题也避免继续贬值有关。虽然今日早间日本央行公布的利率会议纪要仍然是支持宽松政策,但这符…...

    2024/4/27 17:58:04
  10. 【原油贵金属早评】欧佩克稳定市场,填补伊朗问题的影响

    原标题:【原油贵金属早评】欧佩克稳定市场,填补伊朗问题的影响近日伊朗局势升温,导致市场担忧影响原油供给,油价试图反弹。此时OPEC表态稳定市场。据消息人士透露,沙特6月石油出口料将低于700万桶/日,沙特已经收到石油消费国提出的6月份扩大出口的“适度要求”,沙特将满…...

    2024/4/27 14:22:49
  11. 【外汇早评】美欲与伊朗重谈协议

    原标题:【外汇早评】美欲与伊朗重谈协议美国对伊朗的制裁遭到伊朗的抗议,昨日伊朗方面提出将部分退出伊核协议。而此行为又遭到欧洲方面对伊朗的谴责和警告,伊朗外长昨日回应称,欧洲国家履行它们的义务,伊核协议就能保证存续。据传闻伊朗的导弹已经对准了以色列和美国的航…...

    2024/4/28 1:28:33
  12. 【原油贵金属早评】波动率飙升,市场情绪动荡

    原标题:【原油贵金属早评】波动率飙升,市场情绪动荡因中美贸易谈判不安情绪影响,金融市场各资产品种出现明显的波动。随着美国与中方开启第十一轮谈判之际,美国按照既定计划向中国2000亿商品征收25%的关税,市场情绪有所平复,已经开始接受这一事实。虽然波动率-恐慌指数VI…...

    2024/4/30 9:43:09
  13. 【原油贵金属周评】伊朗局势升温,黄金多头跃跃欲试

    原标题:【原油贵金属周评】伊朗局势升温,黄金多头跃跃欲试美国和伊朗的局势继续升温,市场风险情绪上升,避险黄金有向上突破阻力的迹象。原油方面稍显平稳,近期美国和OPEC加大供给及市场需求回落的影响,伊朗局势并未推升油价走强。近期中美贸易谈判摩擦再度升级,美国对中…...

    2024/4/27 17:59:30
  14. 【原油贵金属早评】市场情绪继续恶化,黄金上破

    原标题:【原油贵金属早评】市场情绪继续恶化,黄金上破周初中国针对于美国加征关税的进行的反制措施引发市场情绪的大幅波动,人民币汇率出现大幅的贬值动能,金融市场受到非常明显的冲击。尤其是波动率起来之后,对于股市的表现尤其不安。隔夜美国股市出现明显的下行走势,这…...

    2024/4/25 18:39:16
  15. 【外汇早评】美伊僵持,风险情绪继续升温

    原标题:【外汇早评】美伊僵持,风险情绪继续升温昨日沙特两艘油轮再次发生爆炸事件,导致波斯湾局势进一步恶化,市场担忧美伊可能会出现摩擦生火,避险品种获得支撑,黄金和日元大幅走强。美指受中美贸易问题影响而在低位震荡。继5月12日,四艘商船在阿联酋领海附近的阿曼湾、…...

    2024/4/28 1:34:08
  16. 【原油贵金属早评】贸易冲突导致需求低迷,油价弱势

    原标题:【原油贵金属早评】贸易冲突导致需求低迷,油价弱势近日虽然伊朗局势升温,中东地区几起油船被袭击事件影响,但油价并未走高,而是出于调整结构中。由于市场预期局势失控的可能性较低,而中美贸易问题导致的全球经济衰退风险更大,需求会持续低迷,因此油价调整压力较…...

    2024/4/26 19:03:37
  17. 氧生福地 玩美北湖(上)——为时光守候两千年

    原标题:氧生福地 玩美北湖(上)——为时光守候两千年一次说走就走的旅行,只有一张高铁票的距离~ 所以,湖南郴州,我来了~ 从广州南站出发,一个半小时就到达郴州西站了。在动车上,同时改票的南风兄和我居然被分到了一个车厢,所以一路非常愉快地聊了过来。 挺好,最起…...

    2024/4/29 20:46:55
  18. 氧生福地 玩美北湖(中)——永春梯田里的美与鲜

    原标题:氧生福地 玩美北湖(中)——永春梯田里的美与鲜一觉醒来,因为大家太爱“美”照,在柳毅山庄去寻找龙女而错过了早餐时间。近十点,向导坏坏还是带着饥肠辘辘的我们去吃郴州最富有盛名的“鱼头粉”。说这是“十二分推荐”,到郴州必吃的美食之一。 哇塞!那个味美香甜…...

    2024/4/30 22:21:04
  19. 氧生福地 玩美北湖(下)——奔跑吧骚年!

    原标题:氧生福地 玩美北湖(下)——奔跑吧骚年!让我们红尘做伴 活得潇潇洒洒 策马奔腾共享人世繁华 对酒当歌唱出心中喜悦 轰轰烈烈把握青春年华 让我们红尘做伴 活得潇潇洒洒 策马奔腾共享人世繁华 对酒当歌唱出心中喜悦 轰轰烈烈把握青春年华 啊……啊……啊 两…...

    2024/5/1 4:32:01
  20. 扒开伪装医用面膜,翻六倍价格宰客,小姐姐注意了!

    原标题:扒开伪装医用面膜,翻六倍价格宰客,小姐姐注意了!扒开伪装医用面膜,翻六倍价格宰客!当行业里的某一品项火爆了,就会有很多商家蹭热度,装逼忽悠,最近火爆朋友圈的医用面膜,被沾上了污点,到底怎么回事呢? “比普通面膜安全、效果好!痘痘、痘印、敏感肌都能用…...

    2024/4/27 23:24:42
  21. 「发现」铁皮石斛仙草之神奇功效用于医用面膜

    原标题:「发现」铁皮石斛仙草之神奇功效用于医用面膜丽彦妆铁皮石斛医用面膜|石斛多糖无菌修护补水贴19大优势: 1、铁皮石斛:自唐宋以来,一直被列为皇室贡品,铁皮石斛生于海拔1600米的悬崖峭壁之上,繁殖力差,产量极低,所以古代仅供皇室、贵族享用 2、铁皮石斛自古民间…...

    2024/4/28 5:48:52
  22. 丽彦妆\医用面膜\冷敷贴轻奢医学护肤引导者

    原标题:丽彦妆\医用面膜\冷敷贴轻奢医学护肤引导者【公司简介】 广州华彬企业隶属香港华彬集团有限公司,专注美业21年,其旗下品牌: 「圣茵美」私密荷尔蒙抗衰,产后修复 「圣仪轩」私密荷尔蒙抗衰,产后修复 「花茵莳」私密荷尔蒙抗衰,产后修复 「丽彦妆」专注医学护…...

    2024/4/30 9:42:22
  23. 广州械字号面膜生产厂家OEM/ODM4项须知!

    原标题:广州械字号面膜生产厂家OEM/ODM4项须知!广州械字号面膜生产厂家OEM/ODM流程及注意事项解读: 械字号医用面膜,其实在我国并没有严格的定义,通常我们说的医美面膜指的应该是一种「医用敷料」,也就是说,医用面膜其实算作「医疗器械」的一种,又称「医用冷敷贴」。 …...

    2024/4/30 9:43:22
  24. 械字号医用眼膜缓解用眼过度到底有无作用?

    原标题:械字号医用眼膜缓解用眼过度到底有无作用?医用眼膜/械字号眼膜/医用冷敷眼贴 凝胶层为亲水高分子材料,含70%以上的水分。体表皮肤温度传导到本产品的凝胶层,热量被凝胶内水分子吸收,通过水分的蒸发带走大量的热量,可迅速地降低体表皮肤局部温度,减轻局部皮肤的灼…...

    2024/4/30 9:42:49
  25. 配置失败还原请勿关闭计算机,电脑开机屏幕上面显示,配置失败还原更改 请勿关闭计算机 开不了机 这个问题怎么办...

    解析如下&#xff1a;1、长按电脑电源键直至关机&#xff0c;然后再按一次电源健重启电脑&#xff0c;按F8健进入安全模式2、安全模式下进入Windows系统桌面后&#xff0c;按住“winR”打开运行窗口&#xff0c;输入“services.msc”打开服务设置3、在服务界面&#xff0c;选中…...

    2022/11/19 21:17:18
  26. 错误使用 reshape要执行 RESHAPE,请勿更改元素数目。

    %读入6幅图像&#xff08;每一幅图像的大小是564*564&#xff09; f1 imread(WashingtonDC_Band1_564.tif); subplot(3,2,1),imshow(f1); f2 imread(WashingtonDC_Band2_564.tif); subplot(3,2,2),imshow(f2); f3 imread(WashingtonDC_Band3_564.tif); subplot(3,2,3),imsho…...

    2022/11/19 21:17:16
  27. 配置 已完成 请勿关闭计算机,win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机...

    win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机”问题的解决方法在win7系统关机时如果有升级系统的或者其他需要会直接进入一个 等待界面&#xff0c;在等待界面中我们需要等待操作结束才能关机&#xff0c;虽然这比较麻烦&#xff0c;但是对系统进行配置和升级…...

    2022/11/19 21:17:15
  28. 台式电脑显示配置100%请勿关闭计算机,“准备配置windows 请勿关闭计算机”的解决方法...

    有不少用户在重装Win7系统或更新系统后会遇到“准备配置windows&#xff0c;请勿关闭计算机”的提示&#xff0c;要过很久才能进入系统&#xff0c;有的用户甚至几个小时也无法进入&#xff0c;下面就教大家这个问题的解决方法。第一种方法&#xff1a;我们首先在左下角的“开始…...

    2022/11/19 21:17:14
  29. win7 正在配置 请勿关闭计算机,怎么办Win7开机显示正在配置Windows Update请勿关机...

    置信有很多用户都跟小编一样遇到过这样的问题&#xff0c;电脑时发现开机屏幕显现“正在配置Windows Update&#xff0c;请勿关机”(如下图所示)&#xff0c;而且还需求等大约5分钟才干进入系统。这是怎样回事呢&#xff1f;一切都是正常操作的&#xff0c;为什么开时机呈现“正…...

    2022/11/19 21:17:13
  30. 准备配置windows 请勿关闭计算机 蓝屏,Win7开机总是出现提示“配置Windows请勿关机”...

    Win7系统开机启动时总是出现“配置Windows请勿关机”的提示&#xff0c;没过几秒后电脑自动重启&#xff0c;每次开机都这样无法进入系统&#xff0c;此时碰到这种现象的用户就可以使用以下5种方法解决问题。方法一&#xff1a;开机按下F8&#xff0c;在出现的Windows高级启动选…...

    2022/11/19 21:17:12
  31. 准备windows请勿关闭计算机要多久,windows10系统提示正在准备windows请勿关闭计算机怎么办...

    有不少windows10系统用户反映说碰到这样一个情况&#xff0c;就是电脑提示正在准备windows请勿关闭计算机&#xff0c;碰到这样的问题该怎么解决呢&#xff0c;现在小编就给大家分享一下windows10系统提示正在准备windows请勿关闭计算机的具体第一种方法&#xff1a;1、2、依次…...

    2022/11/19 21:17:11
  32. 配置 已完成 请勿关闭计算机,win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机”的解决方法...

    今天和大家分享一下win7系统重装了Win7旗舰版系统后&#xff0c;每次关机的时候桌面上都会显示一个“配置Windows Update的界面&#xff0c;提示请勿关闭计算机”&#xff0c;每次停留好几分钟才能正常关机&#xff0c;导致什么情况引起的呢&#xff1f;出现配置Windows Update…...

    2022/11/19 21:17:10
  33. 电脑桌面一直是清理请关闭计算机,windows7一直卡在清理 请勿关闭计算机-win7清理请勿关机,win7配置更新35%不动...

    只能是等着&#xff0c;别无他法。说是卡着如果你看硬盘灯应该在读写。如果从 Win 10 无法正常回滚&#xff0c;只能是考虑备份数据后重装系统了。解决来方案一&#xff1a;管理员运行cmd&#xff1a;net stop WuAuServcd %windir%ren SoftwareDistribution SDoldnet start WuA…...

    2022/11/19 21:17:09
  34. 计算机配置更新不起,电脑提示“配置Windows Update请勿关闭计算机”怎么办?

    原标题&#xff1a;电脑提示“配置Windows Update请勿关闭计算机”怎么办&#xff1f;win7系统中在开机与关闭的时候总是显示“配置windows update请勿关闭计算机”相信有不少朋友都曾遇到过一次两次还能忍但经常遇到就叫人感到心烦了遇到这种问题怎么办呢&#xff1f;一般的方…...

    2022/11/19 21:17:08
  35. 计算机正在配置无法关机,关机提示 windows7 正在配置windows 请勿关闭计算机 ,然后等了一晚上也没有关掉。现在电脑无法正常关机...

    关机提示 windows7 正在配置windows 请勿关闭计算机 &#xff0c;然后等了一晚上也没有关掉。现在电脑无法正常关机以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容&#xff0c;让我们赶快一起来看一下吧&#xff01;关机提示 windows7 正在配…...

    2022/11/19 21:17:05
  36. 钉钉提示请勿通过开发者调试模式_钉钉请勿通过开发者调试模式是真的吗好不好用...

    钉钉请勿通过开发者调试模式是真的吗好不好用 更新时间:2020-04-20 22:24:19 浏览次数:729次 区域: 南阳 > 卧龙 列举网提醒您:为保障您的权益,请不要提前支付任何费用! 虚拟位置外设器!!轨迹模拟&虚拟位置外设神器 专业用于:钉钉,外勤365,红圈通,企业微信和…...

    2022/11/19 21:17:05
  37. 配置失败还原请勿关闭计算机怎么办,win7系统出现“配置windows update失败 还原更改 请勿关闭计算机”,长时间没反应,无法进入系统的解决方案...

    前几天班里有位学生电脑(windows 7系统)出问题了&#xff0c;具体表现是开机时一直停留在“配置windows update失败 还原更改 请勿关闭计算机”这个界面&#xff0c;长时间没反应&#xff0c;无法进入系统。这个问题原来帮其他同学也解决过&#xff0c;网上搜了不少资料&#x…...

    2022/11/19 21:17:04
  38. 一个电脑无法关闭计算机你应该怎么办,电脑显示“清理请勿关闭计算机”怎么办?...

    本文为你提供了3个有效解决电脑显示“清理请勿关闭计算机”问题的方法&#xff0c;并在最后教给你1种保护系统安全的好方法&#xff0c;一起来看看&#xff01;电脑出现“清理请勿关闭计算机”在Windows 7(SP1)和Windows Server 2008 R2 SP1中&#xff0c;添加了1个新功能在“磁…...

    2022/11/19 21:17:03
  39. 请勿关闭计算机还原更改要多久,电脑显示:配置windows更新失败,正在还原更改,请勿关闭计算机怎么办...

    许多用户在长期不使用电脑的时候&#xff0c;开启电脑发现电脑显示&#xff1a;配置windows更新失败&#xff0c;正在还原更改&#xff0c;请勿关闭计算机。。.这要怎么办呢&#xff1f;下面小编就带着大家一起看看吧&#xff01;如果能够正常进入系统&#xff0c;建议您暂时移…...

    2022/11/19 21:17:02
  40. 还原更改请勿关闭计算机 要多久,配置windows update失败 还原更改 请勿关闭计算机,电脑开机后一直显示以...

    配置windows update失败 还原更改 请勿关闭计算机&#xff0c;电脑开机后一直显示以以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容&#xff0c;让我们赶快一起来看一下吧&#xff01;配置windows update失败 还原更改 请勿关闭计算机&#x…...

    2022/11/19 21:17:01
  41. 电脑配置中请勿关闭计算机怎么办,准备配置windows请勿关闭计算机一直显示怎么办【图解】...

    不知道大家有没有遇到过这样的一个问题&#xff0c;就是我们的win7系统在关机的时候&#xff0c;总是喜欢显示“准备配置windows&#xff0c;请勿关机”这样的一个页面&#xff0c;没有什么大碍&#xff0c;但是如果一直等着的话就要两个小时甚至更久都关不了机&#xff0c;非常…...

    2022/11/19 21:17:00
  42. 正在准备配置请勿关闭计算机,正在准备配置windows请勿关闭计算机时间长了解决教程...

    当电脑出现正在准备配置windows请勿关闭计算机时&#xff0c;一般是您正对windows进行升级&#xff0c;但是这个要是长时间没有反应&#xff0c;我们不能再傻等下去了。可能是电脑出了别的问题了&#xff0c;来看看教程的说法。正在准备配置windows请勿关闭计算机时间长了方法一…...

    2022/11/19 21:16:59
  43. 配置失败还原请勿关闭计算机,配置Windows Update失败,还原更改请勿关闭计算机...

    我们使用电脑的过程中有时会遇到这种情况&#xff0c;当我们打开电脑之后&#xff0c;发现一直停留在一个界面&#xff1a;“配置Windows Update失败&#xff0c;还原更改请勿关闭计算机”&#xff0c;等了许久还是无法进入系统。如果我们遇到此类问题应该如何解决呢&#xff0…...

    2022/11/19 21:16:58
  44. 如何在iPhone上关闭“请勿打扰”

    Apple’s “Do Not Disturb While Driving” is a potentially lifesaving iPhone feature, but it doesn’t always turn on automatically at the appropriate time. For example, you might be a passenger in a moving car, but your iPhone may think you’re the one dri…...

    2022/11/19 21:16:57