String s = "Arvice";
Class myclass = s.getClass();
System.out.println(myclass.getName()); // prints "java.lang.String"
try {
Class sneakersClass = Class.forName("Sneakers");
} catch (ClassNotFoundException e) {e.printStackTrace();}
interface Shoe {...
}
Shoe shoeish = (Shoe) sneakerClass.newInstance();
// a silly run function that's just looking to be interrupted!
public void run() {
try {
Thread.sleep(5000);
System.out.println("FAIL: No Interrupt! Returned from sleep.");
} catch (InterruptedException e) {
if (isInterrupted()) {
System.out.println("I've been interrupted;"+
" If I'd been doing anything now I'd clean it up.");
return(); // return after finish cleaning up!
}
}
}
// a tight loop would ignore interrupts, so we added a short sleep
public int run() {
int mySum = 0;
for (int iii = 10000000; iii > 0; iii--) {
mySum += iii;
if (iii mod 10000 == 0) {
try {
Thread.sleep(20); // twenty milliseconds!
} catch (InterruptedException e) {
if (isInterrupted()) { // moderately redundant, see below
System.out.println("I've been interrupted;"+
" I only got to " + iii);
return(-1); // return after finish cleaning up!
} // if interrupted
} // catch interrupt
} // once in 10,000 steps check
} // for a long time run a tight loop
return(mySum);
} // method run
//My Thread
transferAmount = ATM.getTypedNumber();
float checkingTotal = checking.getBalance();
float savingsTotal = savings.getBalance();
// would really have to catch if this makes the checkingTotal < 0!
checkingTotal -= transferAmount;
savingsTotal += transferAmount;
checking.setBalance(checkingTotal);
savings.setBalance(savingsTotal);
// My Partner's Thread
withdrawalAmount = ATM.getTypedNumber();
float checkingTotal = checking.getBalance();
// would really have to catch if this makes the checkingTotal < 0!
checkingTotal -= withdrawalAmount;
ATM.squirtMoneyOut(withdrawelAmount);
checking.setBalance(checkingTotal);
public synchronized float debitAccount (Account a, float amount) {
if (a.getBalance - amount < 0) {
throw new BalanceLTZeroException ("some clever message");
}
a.setBalance(a.getBalance() - amount);
return (a.getBalance());
}
float checkingTotal;
synchronize (checking) {
checkingTotal = checking.balance();
checkingTotal -= transferAmount;
checking.balance(checkingTotal);
}
System.Out.printline("You have "+ checkingTotal +" in your checking account");
//My Thread
synchronize (checking) {
synchronize(savings) {
// do stuff to our accounts...
}
}
// My Partner's Thread
synchronize (savings) {
synchronize(checking) {
// do stuff to our accounts...
}
}