Thread doesn't interrupt!? HELP please

C

contrex

Chris,

thank you very much for your great help!
Even if your code isn't very complex I don't understand why we can't
it make easier:
I took a boolean "stopUpdate" as nos proposed to do. As I mentioned
above his solution wouldn't work so I took a second boolean
"isUpdating" which is true while the thread is working and false if
the thread does nothing.

If I want to interrupt the thread then the main program sets
"stopUpdate" to true and it will wait while the thread is still
working ("isUpdate" is true). The thread will exit its time consuming
loop at the next step, enter in the finally clause and sets
"isUpdating" to false.
The main program now continues with it execution.
The only problem I see is that the main program can "freeze" for a
while while waiting the thread to terminate. But in reality this takes
less than 100 milliseconds! So it is not a problemm for my
application.

Or do you see any problems with it?
Can I factorize it (see below my detailed question please)?

public class MyClass extends JFrame{

File []images=null;
Thread updateThread=null;
MyThumb []allThumbs=null;
boolean stopUpdate=false;
boolean isUpdating=false;

//method for creating thumbs by a thread
public void updateThumbs() {

if(updateThread == null){
updateThread = new Thread() {
public void run(){
isUpdating=true;
try{
if(images != null){
allThumbs=new MyThumb[images.length];
for(int x=0; x<allThumbs.length && !stopUpdate; x++){
allThumbs[x]=new MyThumb(images[x]);//no more CRASH
}
}
}catch(Exception ex){
ex.printStackTrace(System.out);
}finally{
isUpdating=false;
}
}
};
updateThread.start();
}
}


//get files of an directory
public void getImages(String folder) {

if(updateThread!=null){
try{
stopUpdate=true;
while (isUpdating) {
wait(100);
}
updateThread = null;

}catch(InterruptedException ex){
//
}
updateThread = null;
}

File f = new File(folder);
images = f.listFiles(new FileFilter() {...};)//get image files
}
}

I have yet another question how to factorize this code:
In order to keep the code clean I tried to create a class
"ThumbsUpdater" which should do the same thing as the current method
"updateThumbs". Moreover I would encapsulate the two booleans
"isUpdating" and "stopUpdate" and provide getter/setter methods for
it.
In the main program I would use it as:

//starting the thread
updateThread = new ThumbsUpdater(this);
updateThread.start();

//stopping the thread
updateThread.stopUpdate();
while (updateThread.isUpdating()) {
wait(100);
}

But this doesn't compile saying "cannot resolve symbol". The class
Thread isn't final so I can extend it by adding some extra methods,
no?
If I don't call the methods "stopUpdate" and "isUpdating" java
compiles fine (even if I extend the class with both methods).
Is it normal? How do you extends a thread? Or do you never call
another method than "run" (start) from outside of a thread? If this
were true why one can extends a thread by "public" methods and not
only by "private" methods? How do you call "public" methods of an
extended thread class by another class without having compile errors?

Thanks in advance!
 
C

Chris Smith

Hi contrex,
Even if your code isn't very complex I don't understand why we can't
it make easier:
I took a boolean "stopUpdate" as nos proposed to do. As I mentioned
above his solution wouldn't work so I took a second boolean
"isUpdating" which is true while the thread is working and false if
the thread does nothing.

For one thing, if you're updating shared state (and these two variable
are shared state), you *have* to synchronize access. That your code
doesn't do so below is a particularly nasty bug because whether it even
happens or not is likely to depend on the target platform and minute
timing details. That's inviting really nasty debugging sessions in the
future.

So you can make that change, which is fairly trivial...
If I want to interrupt the thread then the main program sets
"stopUpdate" to true and it will wait while the thread is still
working ("isUpdate" is true). The thread will exit its time consuming
loop at the next step, enter in the finally clause and sets
"isUpdating" to false.
The main program now continues with it execution.

Theoretically, yes. Your code appears suspect below, but I can't give a
definite answer on race conditions and such because you've left out
important pieces of the code (such as where updateThumbs is actually
called).
The only problem I see is that the main program can "freeze" for a
while while waiting the thread to terminate. But in reality this takes
less than 100 milliseconds! So it is not a problemm for my
application.

Nevertheless, there are general good practices that add up to better
code. One of those is to keep any waiting outside of the AWT event
thread. 100 milliseconds may not look like much, but it's over twice
the delay that the human brain is capable of noticing, and over ten
times the level of delay that makes your program seem nebulously
sluggish if it happens all the time.

Similarly, using interrupt() to stop a task is a good practice; it
doesn't hurt you here (in fact, you would have avoided one of the race
conditions with trying to use boolean flags if you'd done so), and it
helps a lot in very similar situations with a few differences.
In the main program I would use it as:

//starting the thread
updateThread = new ThumbsUpdater(this);
updateThread.start();

//stopping the thread
updateThread.stopUpdate();
while (updateThread.isUpdating()) {
wait(100);
}

But this doesn't compile saying "cannot resolve symbol".

Can't resolve what symbol? You haven't given enough information to
troubleshoot the problem. Full compile errors would be good, plus some
hint about what lines are being referred to by line numbers in the
compiler messages.
The class
Thread isn't final so I can extend it by adding some extra methods,
no?

You can, yes. It's generally considered better form to implement
Runnable and use the standard Thread class, rather than subclassing
Thread.
If I don't call the methods "stopUpdate" and "isUpdating" java
compiles fine (even if I extend the class with both methods).
Is it normal?

You're calling those methods using the reference 'updateThread'. How do
you declare that reference, and what does the class look like that
provudes the type to that variable? That would be your problem.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top