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

W

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
 
H

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
this
wiki.
 
W

Wisdo

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
this
wiki.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top