Question

The main question is that within "//does lots of stuff below" why does the person who originally wrote this check

if (stopscript)
    return;

Will that actually help anything? Doesn't the stopscript() call from Obs guarantee that the thread will be stopped? When I read things like http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html I don't get where the work actually is done. In their run() methods I am not sure where I would insert the actual work to be done.

I have a class Obs in which I have a local Class Scripter and use it like this:

Scripter sscript;
Thread scriptthread;

It has the following (not the whole class), just bits of it::

Instantiation of Scripter within Obs:

   sscript = new Scripter(...);
   scriptthread = new Thread(exesscript);
   Obsrunning = true;
   scriptthread.start();

These are other methods in Obs that are called when STOP or END buttons are clicked.

public void stopscript()
{
    sscript.stopscript();

    if (obssequencepaused)
    {
        sscript.resumescript();
        JBpause.setText("PAUSE");
    } 
   sscript = null;


}

public void endscript()
{

    sscript.endscript();

    if (obssequencepaused)
    {
        sscript.resumescript();
        JBpause.setText("PAUSE");
    }
}

Scripter class info...

public class Scripter implements Runnable
{

    @Override
    public void run()
    {
        //does lots of stuff
    }

    final public void stopscript()
    {
        this.stopscript = true;
        scriptthread.interrupt();
    }

    final public void pausescript()
    {
        this.pausescript = true;
    }

    final public void resumescript()
    {
        this.pausescript = false;
    }

    // sets parameters so loops should just fall through and main run() finishes
    final public void endscript()
    {

        endscript = true;
        numpositions = 0;
        firstRepeats = 1;
        repeats = 1;
    }
}
Était-ce utile?

La solution

The question and code details are a bit vague and that makes it hard to understand very specifically what you are asking for and need help with but I'll try to infer.

When an instance of your class Scripter is started (on Java thread), it will execute the code in the run() method (this is where the 'work' takes place) when the thread is allocated CPU resources. It runs like any other method and if it returns or completes, then the thread is finished and it will be cleaned up.

Thus if the following code:

if (stopscript)
    return;

is called in run() method, then if that condition is true, the return terminates the run() method and the thread can terminate and clean up safely (presumably, correctness depends on what your program needs to do and how you are handling concurrency).

If you just call stop() on the thread running the instance of Scripter, it stops it in whatever state it is in i.e. it could be half-way through a calculation or may not properly clean up its resources usage as detailed in the Javadoc you read.

It essentially makes your program less predictable because the thread isn't allowed to terminate gracefully and that is (can be) problematic.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top