Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / July 2006

Tip: Looking for answers? Try searching our database.

Thread concept

Thread view: 
Greg - 21 Jul 2006 12:22 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.