C
contrex
My program let the user choose a directory.
My program gets all image files of this directory and stores its
references in an array called "images".
Then I start a thread (the one and only) which iterates through the
array in order to create a thumnail for each image. This can take a
while.
When the user selects another directory I interrupt the thread (if
still running) and set it to null BEFORE fetching the new file
references in the array. See method "getImages" below please.
The problem is that the thread hadn't stopped yet! This causes
problems because the thread is still in the loop going from 0 until
images.length and tries to access the array at a position which is no
more valid (assuming the user selected a directory having fewer files
as the previously selected directory and the thread is working on one
of the last images).
The really strange thing is that I see in my log that the thread is
interupted (and even set to null), the thread is not restartet (seeing
in my logs too) and nevertheless the thread accessed the array! How it
is possible??
I even added a parameter to the constructor of "MyThumb" in order to
identify the caller. Each caller has to identify itself. So I see who
is calling the method. And I see it is the thread who calls the method
even if it was setted to null according to my logs and was not
restarted.
Can you explain me this please?
How to resolve this?
Thank for a hint!
Here my simplified code :
public class MyClass extends JFrame{
File []images=null;
Thread updateThread=null;
MyThumb []allThumbs=null;
//method for creating thumbs by a thread
public void updateThumbs() {
if(updateThread == null){
updateThread = new Thread() {
public void run(){
try{
if(images != null){
allThumbs=new MyThumb[images.length];
for(int x=0; x<allThumbs.length; x++){
allThumbs[x]=new MyThumb(images[index]);//CRASH
}
}
}
catch(Exception ex){
ex.printStackTrace(System.out);
}
finally{
updateThread = null;
}
}
};
updateThread.start();
}
}
//get files of an directory
public void getImages(String folder) {
if(updateThread!=null){
updateThread.interrupt();
updateThread = null;
}
File f = new File(folder);
images = f.listFiles(new FileFilter() {...}//get image files
}
}
My program gets all image files of this directory and stores its
references in an array called "images".
Then I start a thread (the one and only) which iterates through the
array in order to create a thumnail for each image. This can take a
while.
When the user selects another directory I interrupt the thread (if
still running) and set it to null BEFORE fetching the new file
references in the array. See method "getImages" below please.
The problem is that the thread hadn't stopped yet! This causes
problems because the thread is still in the loop going from 0 until
images.length and tries to access the array at a position which is no
more valid (assuming the user selected a directory having fewer files
as the previously selected directory and the thread is working on one
of the last images).
The really strange thing is that I see in my log that the thread is
interupted (and even set to null), the thread is not restartet (seeing
in my logs too) and nevertheless the thread accessed the array! How it
is possible??
I even added a parameter to the constructor of "MyThumb" in order to
identify the caller. Each caller has to identify itself. So I see who
is calling the method. And I see it is the thread who calls the method
even if it was setted to null according to my logs and was not
restarted.
Can you explain me this please?
How to resolve this?
Thank for a hint!
Here my simplified code :
public class MyClass extends JFrame{
File []images=null;
Thread updateThread=null;
MyThumb []allThumbs=null;
//method for creating thumbs by a thread
public void updateThumbs() {
if(updateThread == null){
updateThread = new Thread() {
public void run(){
try{
if(images != null){
allThumbs=new MyThumb[images.length];
for(int x=0; x<allThumbs.length; x++){
allThumbs[x]=new MyThumb(images[index]);//CRASH
}
}
}
catch(Exception ex){
ex.printStackTrace(System.out);
}
finally{
updateThread = null;
}
}
};
updateThread.start();
}
}
//get files of an directory
public void getImages(String folder) {
if(updateThread!=null){
updateThread.interrupt();
updateThread = null;
}
File f = new File(folder);
images = f.listFiles(new FileFilter() {...}//get image files
}
}