How to destroy a Thread in J2ME

L

Louis Cyphre

Hi,
I have a question about the destruction of a Thread in J2ME.
Suppose I have a Thread running a ServerSocket to accept incoming
connections and I have to destroy it at a certain moment.
I can use a boolean variable to stop it as in the code:

public void run() {

/* Creates the ServerSocket */
serverSocket = new ServerSocket( port );

/* Keep accepting connections */
while( !quit ) {

/* Grabs the next incoming connection */
Socket socket = serverSocket.accept();

/* and so on... */

}
}

public void quit() {
quit = true;
}

But... What if the Thread is waiting on the .accept() call?
How can I stop it?
If I cannot stop the Thread during the accept(), can I destroy the
Threaf object? How can I do it?
 
O

optinel

Do you think that sending an interrupt on the thread after you set the
quit variable to true could solve the issue?
 
D

Daniel Dyer

Hi,
I have a question about the destruction of a Thread in J2ME.
Suppose I have a Thread running a ServerSocket to accept incoming
connections and I have to destroy it at a certain moment.
I can use a boolean variable to stop it as in the code:

public void run() {

/* Creates the ServerSocket */
serverSocket = new ServerSocket( port );

/* Keep accepting connections */
while( !quit ) {

/* Grabs the next incoming connection */
Socket socket = serverSocket.accept();

/* and so on... */

}
}

public void quit() {
quit = true;
}

But... What if the Thread is waiting on the .accept() call?
How can I stop it?
If I cannot stop the Thread during the accept(), can I destroy the
Threaf object? How can I do it?

If you close the ServerSocket after setting the quit flag to true, the
accept method will throw an IOException, which will allow you to terminate
your loop promptly. Just make sure that quit is declared to be volatile.

Dan.
 
L

Louis Cyphre

Daniel said:
If you close the ServerSocket after setting the quit flag to true, the
accept method will throw an IOException, which will allow you to
terminate your loop promptly. Just make sure that quit is declared to
be volatile.

Dan.
Well... Thank you very much. Your solution sounds good for my problem.
Just another little question... Can you explain me the meaning of the
volatile keyword? I never used it before...

Thanks in advance

Louis
 
D

Darryl Pierce

Louis said:
Hi,
I have a question about the destruction of a Thread in J2ME.
Suppose I have a Thread running a ServerSocket to accept incoming
connections and I have to destroy it at a certain moment.

Close the connection with ServerSocketConnection.close()
 
D

Daniel Dyer

Well... Thank you very much. Your solution sounds good for my problem.
Just another little question... Can you explain me the meaning of the
volatile keyword? I never used it before...

Thanks in advance

Louis

Basically it makes sure that different threads always see the most
up-to-date value of a field, rather than a (possibly out-of-date) cached
value. It's explained a bit better here
(http://mindprod.com/jgloss/volatile.html) and here
(http://www.javaperformancetuning.com/news/qotm030.shtml).

If you don't use it in your code it's possible that your while loop will
iterate one or more times more than you would otherwise expect it to.
Whether that will cause problems depends on what the body of your loop is
doing.

Dan.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top