Suspending and resuming a thread

My applet starts a thread which all it does is wait for a few seconds and then notify the applet it has finished running. In the applet im able to stop the thread while its waiting, and then resume it, and i want the thread to finish whats left from its waiting time
and finish its running. The waiting of the thread is done by sleep. my problem is that resuming the suspended thread doesnt seem to have an effect on it.

 
asafhzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mbormannCommented:
>>>>>The waiting of the thread is done by sleep.
this is not good ,use Object.wait()

>>>>>my problem is that resuming the suspended thread doesnt seem to have an effect on it.

can u post code?

Anyway here's a nice example from Bruce Eckel's 'Thinking in Java'
I think this closely models ur requirement
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Suspend extends Applet
{
    private TextField t = new TextField(10);

    private Button suspend = new Button("Suspend");
    private Button resume = new Button("Resume");

    private Suspendable ss = new Suspendable(t);

    public void init()
    {
        add(t);
        suspend.addActionListener(new ActionListener(){
                                    public void actionPerformed(ActionEvent e)
                                    {
                                        ss.fauxSuspend();
                                    }
                                  });
        add(suspend);
        resume.addActionListener(new ActionListener(){
                                     public void actionPerformed(ActionEvent e)
                                     {
                                         ss.fauxResume();
                                     }
                                                  });
        add(resume);
    }

    public static void main(String[] args)
    {
        Suspend applet = new Suspend();
        Frame aFrame = new Frame("Suspend");
        aFrame.addWindowListener(
            new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    System.exit(0);
                }
            }
        );
        aFrame.add(applet, BorderLayout.CENTER);
        aFrame.setSize(300,100);
        applet.init();
        applet.start();
        aFrame.setVisible(true);
    }
}

class Suspendable extends Thread
{
    private int count = 0;
    private boolean suspended = false;
    private TextField t;

    public Suspendable(TextField t)
    {
        this.t=t;
        start();
    }
    public void fauxSuspend()
    {
        suspended = true;
    }
    public synchronized void fauxResume()
    {
        suspended = false;
        notify();
    }
    public void run()
    {
        while (true)
        {
            try
            {
                sleep(100);
                synchronized(this)
                {
                    while(suspended)
                        wait();
                }
            }
            catch (InterruptedException e)
            {}

            t.setText(Integer.toString(count++));
        }
    }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ravindra76Commented:

Its Fine Amit.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

rainmalCommented:
What "Is Fine" ravindra??
0
rainmalCommented:
well asafhz you cannot call resume on a thread once you call stop on it.

When a thread is stopped, the state of the thread object is set so that it is no longer restartable.

To start and stop a thread use Thread.suspend() to suspend a thread and Thread.resume() to resume execution of a suspended thread
0
rainmalCommented:
However using suspend() and resume() is terribly dangerous as suspend leads to deadlock conditions sometimes ( the very reason that suspend() resume and stop() are now deprecated).

What you can do is let the tread call wait() on itself probably by detecting a flag that has been set by another thread.

in other words, no thread should Stop or suspend another thread.The thread should stop either by returning from its run method or can pause by calling wait() on itself...for safer and more predicatible execution.
0
asafhzAuthor Commented:
thanks mbormann. exactly solved
my problem.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.