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?
 
mbormannConnect With a Mentor Commented:
>>>>>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
 
Ravindra76Commented:

Its Fine Amit.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
All Courses

From novice to tech pro — start learning today.