Hi,
I am not experienced with threads and have question about the correct
approach. In my application I want to read a file (takes about 10 seconds)
in a thread. So in my class UserController I have crated new class for
Thread called LoadFile. Once thread finish readFile() method that is defined
in it, it calls method loadComplete() from class UserController. I think it
is not good way to inform userGUI that loading is completed by calling from
thread method that in UserController. Also if UserController object will be
disposed then thread will try to call something that should not exists.
Could you please advice me what is the best approach of using thread in this
scenario?
Many thanks in advance,
Greg
public class LoadFile implements Runnable
{
public LoadFile(String filePath)
{
this.filePath = filePath;
}
private void readFile()
{
// body
}
public void run()
{
try
{
readFile ();
loadComplete();
// public method in class UserController
}
catch(Exception ex)
{
}
}
public void start()
{
// loadThread is defined in
UserController (private Thread loadThread;)
if (loadThread == null)
{
loadThread =
new Thread(this);
loadThread.start();
}
}
public void stop()
{
if (loadThread != null)
{
Thread r =
loadThread;
loadThread =
null;
r.interrupt
(); //
}
}
Greg - 21 Jul 2006 13:00 GMT
Sorry for formatting :)
public class LoadFile implements Runnable{
public LoadFile(String filePath){
this.filePath = filePath;
}
private void readFile(){
// body
}
public void run(){
try{
readFile ();
loadComplete(); //
public method in class UserController
}
catch(Exception ex){
}
}
public void start(){
// loadThread is defined in UserController
(private Thread loadThread;)
if (loadThread == null){
loadThread = new
Thread(this);
loadThread.start();
}
}
public void stop() {
if (loadThread != null){
Thread r = loadThread;
loadThread = null;
r.interrupt (); //
}
}
}
Fahd Shariff - 24 Jul 2006 16:12 GMT
I think the Java Observer Pattern will fit your requirements very
nicely.

Signature
Fahd Shariff
Greg - 25 Jul 2006 10:35 GMT
Looks good!!
Thanks a lot
Greg
>I think the Java Observer Pattern will fit your requirements very
> nicely.