62,634
社区成员




public class Account {
int amount;
Account() {
}
public synchronized void deposit() {
amount += 1;
}
}
public class AccountWithSync {
Account myaccount = new Account();
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
executor.execute(new addOnePenny());
}
}
}
class addOnePenny implements Runnable{
@Override
public void run() {
}
}
public class AccountWithSync {
static Account myaccount = new Account();
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
executor.execute(new addOnePenny(myaccount ));
}
}
}
class addOnePenny implements Runnable{
private Account account;
public addOnePenny(Account account){
this.account = account;
}
@Override
public void run() {
account.deposit();
}
}