java编程题:100个线程同时向一个银行账户中存入1元钱

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * java编程题:100个线程同时向一个银行账户中存入1元钱 
 */
public class Test {

	public static void main(String[] args) {
		Account account = new Account();
		ExecutorService service = Executors.newFixedThreadPool(100);
		
		for (int i = 1; i <= 100; i++) {
			service.execute(new MoneyThread(account, 1));
		}
		
		service.shutdown();
		
		while(!service.isTerminated()){}
		
		System.out.println("账户金额:"+account.getBalance());
	}

}

/**
 * 银行账户类
 */
class Account {
	
	private double balance;			//账户余额
	
	private Lock accountLock = new ReentrantLock();			//锁对象
	
	/**
	 * 存入金额方法
	 * 
	 * 实现方式1:
	 * 	  synchronized关键字修饰方法为同步方法
	 *
	public synchronized void deposit(double money) {
		double newBalance = balance + money;
		
		t

你可能感兴趣的:(Java编程算法)