(使用 lock 类和多线程管理newCondition/wait/signal/signalAll 实现) 假设创建并启动两个任务,一个用来向账户中存款,存入一个随机金额 (Math.random()100),另外一个从同一账户中提款,提款一个随机金额(Math.random()100)。当提款的数额大于账户的当前余额时,提款线程必须等待。不管什么时候,只要向账户新存入一笔资金,存储线程必须通知提款线程重新尝试。如果余额仍未达到提款的数额,提款线程必须继续等待新的存款。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Demo {
private static Account account = new Account();
public static void main(String[] args) {
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 {
public void run() {
try {
while (true) {
account.deposit((int) (Math.random() * 10) + 1);
Thread.sleep(1000);
}
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public static class WithdrawTask implements Runnable {
public void run() {
while (true) {
account.withDraw((int) (Math.random() * 10) + 1);
}
}
}
public static class Account {
private static Lock lock = new ReentrantLock();
private static Condition newDeposit = lock.newCondition();
private int balance = 0;
public int getBalance() {
return balance;
}
public void withDraw(int amount) {
lock.lock();
try {
while (balance < amount) {
newDeposit.await();
}
balance -= amount;
System.out.println("\t\t\tWithdraw " + amount + "\t\t" + getBalance());
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
finally {
lock.unlock();
}
}
public void deposit(int amount) {
lock.lock();
try {
balance += amount;
System.out.println("Deposit " + amount + "\t\t\t\t\t" + getBalance());
newDeposit.signalAll();
}
finally {
lock.unlock();
}
}
}
}*/
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
public class Main1 {
private static Account account = new Account();
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(2);
System.out.println("Thread 1\t\tThread 2\t\t\t\tBalance");
executor.execute(new DepositTask());
executor.execute(new WithdrawTask());
executor.shutdown();
}
public static class DepositTask implements Runnable {
@Override
public void run() {
try{
while(true) {
account.deposit((int)(Math.random() * 10) + 1);
Thread.sleep(1000);
}
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
public static class WithdrawTask implements Runnable {
@Override
public void run() {
while(true) {
account.withdraw((int)(Math.random() * 10) + 1);
}
}
}
private static class Account {
private static Lock lock = new ReentrantLock();
private static Condition newDeposit = lock.newCondition();
private int balance = 0;
public int getBalance() {
return balance;
}
public void withdraw(int amount) {
lock.lock();
try{
while(balance < amount) {
System.out.println("\t\t\tWait for a deposit.");
newDeposit.await();
}
balance -= amount;
System.out.println("\t\t\tWithdraw " +
amount + "\t\t\t\t" + getBalance());
} catch (InterruptedException ex) {
ex.printStackTrace();
} finally {
lock.unlock();
}
}
public void deposit(int amount) {
lock.lock();
try{
balance += amount;
System.out.println("Deposit " +
amount + "\t\t\t\t\t\t\t\t" + getBalance());
newDeposit.signal();
} catch (IllegalMonitorStateException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
}
|