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 / General / August 2006

Tip: Looking for answers? Try searching our database.

is that OK if i make the Thread object as member variable?

Thread view: 
Wisdo - 21 Aug 2006 04:12 GMT
Hi ALL,

I write a Listener class, and let the Thread obj
as member variable,    when initilize and destroy
call corresponding thread method.

have it some implicit problem ?
whether i need add synchronized keyword into
some places?

please give your salted suggestion.

-------- code begin ----------------------
public Listener extends Runnable {

    private Thread th;

    public void initilize() {
           th = new Thread(this);
           th.setName(getClass().getName());
           th.setDaemon(true);
           th.start();
    }

    public    void run() {
        //listen evnet and perform operation
    }

    public void destry() {
        th.stop();
    }
}
---------- code end --------------------

Thanks
-Wisdo
hiwa - 21 Aug 2006 04:20 GMT
Wisdo のメッセージ:

> Hi ALL,
>
[quoted text clipped - 32 lines]
> Thanks
> -Wisdo
Your code is wrong in a few places.
Show your real usage. Post a small demo code that is generally
compilable, runnable and could reproduce your problem. See:
http://homepage1.nifty.com/algafield/sscce.html and
[url=http://riters.com/JINX/index.cgi/Suggestions_20for_20Asking_20Questions_20on_20N
ewsgroups]this

wiki.[/url]
Wisdo - 21 Aug 2006 05:10 GMT
hiwa 写道:
> Wisdo のメッセージ:
>
[quoted text clipped - 40 lines]
> [url=http://riters.com/JINX/index.cgi/Suggestions_20for_20Asking_20Questions_20on_20N
ewsgroups]this

> wiki.[/url]

thanks for the inform.

the new code,  is any synchronzied suggestion?

package test.misc;

public class Listener implements Runnable {
   
     private Thread th;

     public void initilize() {
        th = new Thread(this);
        th.setName(getClass().getName());
        th.setDaemon(true);
        th.start();
     }

     public    void run() {
         while(true)    System.out.println("get event, processing....");
     }

     public void destroy() {
         th.stop();
     }
    
    
     public static void main(String args[]) throws InterruptedException {
         Listener lst = new Listener();
         lst.initilize();
        
         Thread.sleep(10000);
         lst.destroy();
     }
}

Regards
-Wisdo


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.