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!
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() {...}
}
}
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!