Joker
Joker

Reputation: 91

Thread End Determination

The following thread runs on a button click.

open.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        calls();
    }
});


private void calls() {
    Thread t = new Thread(this);
    t.start();
}

@Override
public void run() {
    reads();
}

public void reads() {
    ....
    ....
    counter++;
    // The Thread should stop here right>
}

But it doesn't end there. I am using thread.stop() to stop the thread there. I know the range the counter has to go. I display counter value in UI. If I click the open button once the counter reaches the end state, then it shows an Interrupted exception.

Upvotes: 1

Views: 53

Answers (1)

AloneInTheDark
AloneInTheDark

Reputation: 938

Define your t object outside the function, maybe in the constructor. Then you can stop it with this:

 t.interrupt();

EDIT

Thread t;
public myclass()//your constructor
{
t=new Thread(this);
}

    open.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            calls();
        }
    });


    private void calls() {
        t.start();
    }

    @Override
    public void run() {
        reads();
    }

    public void reads() {
        ....
        ....
        counter++;
t.interrupt();// The Thread should stop here right>

    }

Upvotes: 1

Related Questions