package chapter29;import java.util.concurrent.*;public class AccountWithSync {
private static Account account = new Account(); public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
AddAPennyTask ta = new AddAPennyTask();
MinuTask tb = new MinuTask();
// Create and launch 10 threads
for (int i = 0; i < 10; i++) {
executor.execute(new AddAPennyTask());
}
if(account.getBalance() >= 5) {
executor.execute(new MinuTask());
} executor.shutdown(); // Wait until all tasks are finished
while (!executor.isTerminated()) {
} System.out.println("What is balance? " + account.getBalance());
} // A thread for adding a penny to the account
private static class AddAPennyTask implements Runnable {
public void run() {
account.deposit(1);
System.out.println("add 1");
}
}
private static class MinuTask implements Runnable {
public void run() {
account.takeout(3);
System.out.println("take out 3.");
}
}
// An inner class for account
private static class Account {
private int balance = 0; public int getBalance() {
return balance;
} public synchronized void deposit(int amount) {
int newBalance = balance + amount;
// This delay is deliberately added to magnify the
// data-corruption problem and make it easy to see.
try { //只有去掉takeout()才有机会抢到资源
}
catch (InterruptedException ex) {
}
balance = newBalance;
}
public synchronized void takeout(int amount) {
int newBalance = balance - amount;
try { //去掉后只能在最后两次中抢到资源
Thread.sleep(100);
}
catch (InterruptedException ex) {
}
balance = newBalance;
}
}
}
private static Account account = new Account(); public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
AddAPennyTask ta = new AddAPennyTask();
MinuTask tb = new MinuTask();
// Create and launch 10 threads
for (int i = 0; i < 10; i++) {
executor.execute(new AddAPennyTask());
}
if(account.getBalance() >= 5) {
executor.execute(new MinuTask());
} executor.shutdown(); // Wait until all tasks are finished
while (!executor.isTerminated()) {
} System.out.println("What is balance? " + account.getBalance());
} // A thread for adding a penny to the account
private static class AddAPennyTask implements Runnable {
public void run() {
account.deposit(1);
System.out.println("add 1");
}
}
private static class MinuTask implements Runnable {
public void run() {
account.takeout(3);
System.out.println("take out 3.");
}
}
// An inner class for account
private static class Account {
private int balance = 0; public int getBalance() {
return balance;
} public synchronized void deposit(int amount) {
int newBalance = balance + amount;
// This delay is deliberately added to magnify the
// data-corruption problem and make it easy to see.
try { //只有去掉takeout()才有机会抢到资源
}
catch (InterruptedException ex) {
}
balance = newBalance;
}
public synchronized void takeout(int amount) {
int newBalance = balance - amount;
try { //去掉后只能在最后两次中抢到资源
Thread.sleep(100);
}
catch (InterruptedException ex) {
}
balance = newBalance;
}
}
}
解决方案 »
- 麻烦大家看看这个是如何实现的?
- 请教一个问题,一个具有静态属性的成员方法能不能被子类覆盖?
- XML节点内容追加怎么实现?将<a>xx</a>变成<a>xxyyyyy</a>,不要setText('xx'+'yyyyy')?
- 一打開QQ的信訊, QQ就死了, 重裝了N次也有這個問題, 我是用QQ2006繁體版的, 怎解決?
- FileWriter写入'\n'到文本文件后,为什么用记事本打开换行会显示成一个小方格?有什么办法解决吗?
- 关于Button的问题
- java读取MDB文件
- GOOD NEWS:Borland与BEA联盟 共谋Java市场领导地位
- 我有一个网页开子页并得到返回值的问题
- 做程序员,选择C# 还是java?如何选择,请给我提示,谢谢!!!
- JTree和JTable问题
- thread dump 代码求指点
throws InterruptedException在指定的毫秒数内让当前正在执行的线程休眠(暂停执行),此操作受到系统计时器和调度程序精度和准确性的影响。该线程不丢失任何监视器的所属权。 import java.util.concurrent.*;public class AccountWithSync
{
private static Account account = new Account(); public static void main(String[] args)
{
ExecutorService executor = Executors.newCachedThreadPool();
AddAPennyTask ta = new AddAPennyTask();
MinuTask tb = new MinuTask();
// Create and launch 10 threads
for (int i = 0; i < 10; i++)
{
executor.execute(new AddAPennyTask());
}
if(account.getBalance() >= 5)
{
executor.execute(new MinuTask());
} executor.shutdown(); // Wait until all tasks are finished
while (!executor.isTerminated())
{ } System.out.println("What is balance? " + account.getBalance());
} // A thread for adding a penny to the account
private static class AddAPennyTask implements Runnable
{
public void run()
{
account.deposit(1);
System.out.println("add 1");
}
}
private static class MinuTask implements Runnable
{
public void run() {
account.takeout(3);
System.out.println("take out 3.");
}
}
// An inner class for account
private static class Account
{
private int balance = 0; public int getBalance()
{
return balance;
} public synchronized void deposit(int amount)
{
int newBalance = balance + amount;
// This delay is deliberately added to magnify the
// data-corruption problem and make it easy to see.
try
{
Thread.sleep(100); //只有去掉takeout()才有机会抢到资源
}
catch (InterruptedException ex)
{
}
balance = newBalance;
}
public synchronized void takeout(int amount)
{
int newBalance = balance - amount;
try { //去掉后只能在最后两次中抢到资源
Thread.sleep(100);
}
catch (InterruptedException ex)
{
}
balance = newBalance;
}
}
}