Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Destroying a Thread

Reply
Thread Tools

Destroying a Thread

 
 
Jason Cavett
Guest
Posts: n/a
 
      04-03-2007
This question is a follow-up to:
http://groups.google.com/group/comp....48f31e4d3ed7/#

I am using the BlockingQueue as suggested in my first post. It works
great (well, still not sure about a few things, but I'm trying to
figure them out). I did figure out that, if I want to stop the
thread, the best way is to return from the run method, so I have
something like this:

/**
* @see java.lang.Runnable#run()
*/
public void run() {
while (true) {
try {
File file = queue.take();

// check to see if the queue has been poisoned
if (file.equals(FileWorker.POISON_FILE)) {
break;
} else {
this.processFile(file);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}

return;
}

That way, if I close down the project, I can put a File object in the
queue that lets the thread know that it is finished (AKA, the project
has closed). (I clear the queue first so that the poison object will
be the only object left in the queue.)

The problem I am having is this - if a process is already running, I
don't really have any way of interrupting it (which I want to do if
the user is closing down the project because I don't want the very
processor intensive process running in the background while they
continue to work).

Any suggestions for stopping a process? (This would also be helpful
because I want to allow the user to choose to cancel a process that
they have already started.)


Thanks

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      04-03-2007
Jason Cavett wrote:
> This question is a follow-up to:
> http://groups.google.com/group/comp....48f31e4d3ed7/#
>
> I am using the BlockingQueue as suggested in my first post. It works
> great (well, still not sure about a few things, but I'm trying to
> figure them out). I did figure out that, if I want to stop the
> thread, the best way is to return from the run method, so I have
> something like this:
>
> /**
> * @see java.lang.Runnable#run()
> */
> public void run() {
> while (true) {
> try {
> File file = queue.take();
>
> // check to see if the queue has been poisoned
> if (file.equals(FileWorker.POISON_FILE)) {
> break;
> } else {
> this.processFile(file);
> }
> } catch (InterruptedException e) {
> e.printStackTrace();
> }
> }
>
> return;
> }
>
> That way, if I close down the project, I can put a File object in the
> queue that lets the thread know that it is finished (AKA, the project
> has closed). (I clear the queue first so that the poison object will
> be the only object left in the queue.)
>
> The problem I am having is this - if a process is already running, I
> don't really have any way of interrupting it (which I want to do if
> the user is closing down the project because I don't want the very
> processor intensive process running in the background while they
> continue to work).
>
> Any suggestions for stopping a process? (This would also be helpful
> because I want to allow the user to choose to cancel a process that
> they have already started.)
>
>
> Thanks
>


public void run() {
try {
while (true) {
????.take();
}
} catch (InterruptedException ie) { }
}

Just interrupt the thread. take() throws an InterruptedException if it
is interrupted while waiting.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
 
Tom Hawtin
Guest
Posts: n/a
 
      04-03-2007
Jason Cavett wrote:
>
> The problem I am having is this - if a process is already running, I
> don't really have any way of interrupting it (which I want to do if
> the user is closing down the project because I don't want the very
> processor intensive process running in the background while they
> continue to work).


I suggest just checking a flag periodically.

You can use Thread.interrupt which should interrupt I/O and any waits.
However, interruptible I/O has been quietly deprecated and interrupts
are more often than not handled incorrectly. Interrupting a thread that
is loading a class can be a bad move (the class may fail to load, and
therefore becomes completely unloadable with that class loader).

Tom Hawtin
 
Reply With Quote
 
Jason Cavett
Guest
Posts: n/a
 
      04-03-2007
On Apr 3, 1:52 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> Jason Cavett wrote:
> > This question is a follow-up to:
> >http://groups.google.com/group/comp....r/browse_frm/t...

>
> > I am using the BlockingQueue as suggested in my first post. It works
> > great (well, still not sure about a few things, but I'm trying to
> > figure them out). I did figure out that, if I want to stop the
> > thread, the best way is to return from the run method, so I have
> > something like this:

>
> > /**
> > * @see java.lang.Runnable#run()
> > */
> > public void run() {
> > while (true) {
> > try {
> > File file = queue.take();

>
> > // check to see if the queue has been poisoned
> > if (file.equals(FileWorker.POISON_FILE)) {
> > break;
> > } else {
> > this.processFile(file);
> > }
> > } catch (InterruptedException e) {
> > e.printStackTrace();
> > }
> > }

>
> > return;
> > }

>
> > That way, if I close down the project, I can put a File object in the
> > queue that lets the thread know that it is finished (AKA, the project
> > has closed). (I clear the queue first so that the poison object will
> > be the only object left in the queue.)

>
> > The problem I am having is this - if a process is already running, I
> > don't really have any way of interrupting it (which I want to do if
> > the user is closing down the project because I don't want the very
> > processor intensive process running in the background while they
> > continue to work).

>
> > Any suggestions for stopping a process? (This would also be helpful
> > because I want to allow the user to choose to cancel a process that
> > they have already started.)

>
> > Thanks

>
> public void run() {
> try {
> while (true) {
> ????.take();
> }
> } catch (InterruptedException ie) { }
>
> }
>
> Just interrupt the thread. take() throws an InterruptedException if it
> is interrupted while waiting.
>
> --
>
> Knute Johnson
> email s/nospam/knute/- Hide quoted text -
>
> - Show quoted text -


But what if I made it to this line...

this.processFile(file);

And now I'm doing the processing of the file (which can take quite a
long time - 10 minutes or more) and I want to interrupt that
processing? That's what I'm curious about. I already know how to
stop the thread if the queue is in waiting state.

 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      04-03-2007
Jason Cavett wrote:
> On Apr 3, 1:52 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
> wrote:
>> Jason Cavett wrote:
>>> This question is a follow-up to:
>>> http://groups.google.com/group/comp....r/browse_frm/t...
>>> I am using the BlockingQueue as suggested in my first post. It works
>>> great (well, still not sure about a few things, but I'm trying to
>>> figure them out). I did figure out that, if I want to stop the
>>> thread, the best way is to return from the run method, so I have
>>> something like this:
>>> /**
>>> * @see java.lang.Runnable#run()
>>> */
>>> public void run() {
>>> while (true) {
>>> try {
>>> File file = queue.take();
>>> // check to see if the queue has been poisoned
>>> if (file.equals(FileWorker.POISON_FILE)) {
>>> break;
>>> } else {
>>> this.processFile(file);
>>> }
>>> } catch (InterruptedException e) {
>>> e.printStackTrace();
>>> }
>>> }
>>> return;
>>> }
>>> That way, if I close down the project, I can put a File object in the
>>> queue that lets the thread know that it is finished (AKA, the project
>>> has closed). (I clear the queue first so that the poison object will
>>> be the only object left in the queue.)
>>> The problem I am having is this - if a process is already running, I
>>> don't really have any way of interrupting it (which I want to do if
>>> the user is closing down the project because I don't want the very
>>> processor intensive process running in the background while they
>>> continue to work).
>>> Any suggestions for stopping a process? (This would also be helpful
>>> because I want to allow the user to choose to cancel a process that
>>> they have already started.)
>>> Thanks

>> public void run() {
>> try {
>> while (true) {
>> ????.take();
>> }
>> } catch (InterruptedException ie) { }
>>
>> }
>>
>> Just interrupt the thread. take() throws an InterruptedException if it
>> is interrupted while waiting.
>>
>> --
>>
>> Knute Johnson
>> email s/nospam/knute/- Hide quoted text -
>>
>> - Show quoted text -

>
> But what if I made it to this line...
>
> this.processFile(file);
>
> And now I'm doing the processing of the file (which can take quite a
> long time - 10 minutes or more) and I want to interrupt that
> processing? That's what I'm curious about. I already know how to
> stop the thread if the queue is in waiting state.
>


You need some sort of test or exception capture in processFile(). If
you have the source just check a flag or use the Thread.interrupted() to
test. If you don't have the source to processFile() then it may be
possible to make the run method use a daemon thread and close your
program to stop it.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Destroying a thread in a threadgroup Frank Cisco Java 1 06-02-2009 05:06 PM
Destroying Sessions Varangian ASP .Net 6 03-29-2006 03:52 PM
Destroying objects in Page_Unload event =?Utf-8?B?dnZlbms=?= ASP .Net 3 11-09-2005 10:48 PM
Problem destroying Process after reading normal and error output. molar Java 0 07-25-2004 09:54 PM
Proper way of destroying unused session objects NETUser2004 ASP .Net 3 02-10-2004 05:05 AM



Advertisments