Do you write well behaving code?

A

Antti S. Brax

I am using a 3rd party library which executes an operation
that at worst may take hours to finish (an XSL transformation).
It processes large XML documents and creates thousands of
files. This library is called from a thread, which has a
"requestStop" method with which it can be stopped cleanly.
Unfortunately, the library never checks whether or not the
thread that runs it has been interrupted.

So, even though my code is correct and it stops right after
the library call finishes, I'm still stuck with a thread that
is practically impossible to stop cleanly.

Therefore, my conclusion is that if you do not periodically
check Thread.interrupted() during time consuming operations
you do not write well behaving code and you are not a good
programmer (I'd put a smiley here if I didn't have to deal
with this problem right now).
 
A

Andrey Kuznetsov

Therefore, my conclusion is that if you do not periodically
check Thread.interrupted() during time consuming operations
you do not write well behaving code and you are not a good
programmer (I'd put a smiley here if I didn't have to deal
with this problem right now).
you can't interrupt _running_ thread.

this used usually to interrupt sleeping or waiting thread:

int sleepTime = 500;

public void run( ) {
while( true ) {
try {
// Sleep's for a specified time
Thread.sleep( sleepTime );
}
catch(InterruptedException ex) {
//we are interrupted - break work
break;
}
//compute something here
}
}
}
So, even though my code is correct and it stops right after
the library call finishes, I'm still stuck with a thread that
is practically impossible to stop cleanly.

library should have some method to stop itself:

boolean canceled;

public void cancelWork() {
canceled = true;
}

public void doHeavyWork() {
while(!workDone) {
doPartOfJob();
if(canceled) {
break;
}
}
}
I am using a 3rd party library which executes an operation
that at worst may take hours to finish (an XSL transformation).
It processes large XML documents and creates thousands of
files. This library is called from a thread, which has a
"requestStop" method with which it can be stopped cleanly.
Unfortunately, the library never checks whether or not the
thread that runs it has been interrupted.

search for another lib or run it as Process.
 
A

Andrea Desole

Andrey said:
you can't interrupt _running_ thread.

Well, this is not completely true. If you interrupt a running thread its
interrupted status will be set. The annoying thing is that the running
thread has to check if it has been interrupted.
 
A

Antti S. Brax

you can't interrupt _running_ thread.

Exactly! How would you interrupt a thread that executes
"while (true) { }"?

You don't, because that code does not behave well. A well
behaving code would be "while (!Thread.interrupted()) { }".

But no worries, I figured out a way to stop the thread.
Because it creates many new files to a directory all I have
to do is to remove the directory where it is writing the
files. As soon as it tries to create a new one it kills
itself with an IOException. Ugly but good enough.
search for another lib or run it as Process.

I'd like a job with no legacy code too... :)
 
A

Andrey Kuznetsov

you can't interrupt _running_ thread.
Well, this is not completely true. If you interrupt a running thread its
interrupted status will be set. The annoying thing is that the running
thread has to check if it has been interrupted.

right, interrupted state is set, but thread is not interrupted.

if you could interrupt running thread then you should write something like:

public void doHeavyWork() {
try {
while(!finished) {
doPartOfJob();
}
}
catch(InterruptedException ex) {
System.out.println("Oh god, again interrupted ;-(");
}
}

or (funny)

public void doHeavyWork() {

while(!finished) {
try {
doPartOfJob();
}
catch(InterruptedException ex) {
if(shouldBreakOnInterrupt) {
break;
}
else {
System.out.println("please don't disturb");
continue;
}
}
}
}
 
D

David Van de Voorde

It seems to me you are mixing 2 problems here:
1. the thread is consuming resources, while you want it to regularly yield
them
2. you want to be able to stop the process pre-maturely in a clean way

Since you're talking about removing the output dir and letting the process
abort with an exception, it seems then to me that you're only concerned
about point 2.

Then, why don't you do smth simple like:

class MyThread extends Thread {
public void run() {
library.startup_and_run();
}

public void requestAbort() {
library.requestStop();
}
}


David.
 
A

Antti S. Brax

It seems to me you are mixing 2 problems here:
1. the thread is consuming resources, while you want it to regularly yield
them
2. you want to be able to stop the process pre-maturely in a clean way

No. I am not interested in yielding resources regularly. The
primary resouce in question is the processor and JVM already
handles the regular yielding of it.
Since you're talking about removing the output dir and letting the process
abort with an exception, it seems then to me that you're only concerned
about point 2.

If I was dealing with a process this would be easy. However, I
am stuck with a Java _thread_ that can not be killed.
Then, why don't you do smth simple like:
public void requestAbort() {
library.requestStop();
}

I don't do that because the library does not have a method for
stopping execution.



I may have overestimated my audience. Sorry. I'm not seeking
advice for my problem because I know there is no way to solve it
(I thought I stated it clear enough). In Java there simply is
_no_ _way_ to kill an _uncooperative_ thread.

I'm trying to provoke some discussion and get you (the programmers)
to realize this problem: don't write uncooperative libraries.



And please don't top post.
 
A

Aquila Deus

Antti said:
Exactly! How would you interrupt a thread that executes
"while (true) { }"?

You don't, because that code does not behave well. A well
behaving code would be "while (!Thread.interrupted()) { }".

But no worries, I figured out a way to stop the thread.
Because it creates many new files to a directory all I have
to do is to remove the directory where it is writing the
files. As soon as it tries to create a new one it kills
itself with an IOException. Ugly but good enough.


Ah, but who to catch the exception (assume that "while" thread just
dies)?
 
A

Antti S. Brax

Ah, but who to catch the exception (assume that "while" thread just
dies)?

Care to elaborate? There is nothing special in the exception
handling here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top