Pausing/Resuming of Threads

Hi, I've noticed that the resume and suspend functions are depracated for the Thread class. It there an easy way to pause threads and restart them? Sorry, If this is really basic... still kind of new to Java.
[223 byte] By [wpr101a] at [2007-11-27 8:34:44]
# 1
You can read the API on why they are deprecated. Then read the API on:Thread.sleep()Object.wait()/notify()java.util.concurrent.locks.condition.await()/signal().
cooper6a at 2007-7-12 20:31:02 > top of Java-index,Core,Core APIs...
# 2

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)?

wpr101a at 2007-7-12 20:31:02 > top of Java-index,Core,Core APIs...
# 3
Calling wait() will cause whatever thread is calling it to wait. If that's the main thread, so be it. If it's the thread you wanted to pause, it is paused. It's all up to you.
ejpa at 2007-7-12 20:31:02 > top of Java-index,Core,Core APIs...
# 4

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

skalvia at 2007-7-12 20:31:02 > top of Java-index,Core,Core APIs...