Thanks for the help. It appears that the Thread.sleep() method takes in a specific unit of time to sleep for which I don't think I want?
Are you implying that i would call myThread.wait() and myThread.notify() to do the sleep awake thing? Someone I talked to a second ago said that would cause my main program to wait (although I didn't get that either because the wait is being called on a specific object)?
Here's an example for you
* SpendSave.java: A variation on the consumer /
*producer problem to illustrate
*fine grained synchronization
*of threads.
public class SpendSave {
AccountBalance Balance; // Account Balance
// Constructor *******************************
public SpendSave()
{
Balance = new AccountBalance(0f);
new Spender("Spender").start(); // Spend a little
new Saver("Saver").start();// Save a little
}
// Saver *************************************
class Saver extends Thread {
Float Amount;
// Constructor
Saver(String name)
{
super(name);
this.Amount = 0f;
}
public void run()
{
deposit(100f);
deposit(200f);
deposit(300f);
deposit(400f);
}
void deposit(Float Amount)
{
System.out.println("deposit is: "
+ Amount
+ "\n");
synchronized (Balance) { // acquire lock
Balance.add(Amount);
System.out.println("deposit balance is: "
+ Balance.getBalance()
+ " ** "
+ Thread.currentThread().getName()
+ "\n");
}// lock released
synchronized (Balance) { // reacquire lock
if (!Balance.insufficient_funds()) {
Balance.notifyAll();
System.out.println("Saver thread notifying "
+ "Spender Thread..."
+ "\n");
} // lock released again
}
}
} // Saver
// Spender ***********************************
class Spender extends Thread {
Float Amount;
// Constructor
Spender(String name)
{
super(name);
this.Amount = 0f;
}
public void run()
{
withdrawal(50f);
withdrawal(100f);
withdrawal(150f);
withdrawal(200f);
}
void withdrawal (Float Amount)
{
System.out.println("withdrawal is:"
+ Amount
+ "\n");
synchronized (Balance) { // acquire lock
Balance.sub(Amount);
System.out.println("withdrawal balance is:"
+ Balance.getBalance()
+ " ** "
+ Thread.currentThread().getName()
+ "\n");
} // lock released
// Insufficient Funds.....
synchronized (Balance) { // reacquire lock
while (Balance.insufficient_funds())
{
System.out.println("Insufficient Funds: "
+ Balance.getBalance()
+ "\n");
try {
System.out.println("Spender Thread Waiting..\n");
Balance.wait(); // Relinquish the lock...
// Let the Saver Thread
// notify Spender when
// the balance is greater than zero
System.out.println("Spender Thread Released..\n");
}
catch (InterruptedException e)
{
System.out.println("Withdrawal permitted"
+ "to continue in case interrupt");
}
//insufficient_funds = false;
}
} // lock released again
}
}
public static void main(String args[])
{
SpendSave spendnsave = new SpendSave();
// Slow up the main thread so
// we can see how many threads are currently active...
try {
Thread.sleep(5000);
}
catch (InterruptedException e) {}
System.out.println("Alive Threads:" + Thread.activeCount());
}
}
/*
* AccountBalance.java: Definition of locked object
*/
public class AccountBalance extends Object {
Float balance;
public AccountBalance( Float amount)
{
this.balance = amount;
}
public Float add(Float amount)
{
this.balance += amount;
return this.balance;
}
public Float sub(Float amount)
{
this.balance -= amount;
return this.balance;
}
public Float getBalance()
{
return this.balance;
}
public boolean insufficient_funds() {
if (this.balance < 0f) return true;
else return false;
}
}
Message was edited by:
skalvi