views:

135

answers:

6

I have a thread:

Thread t = new Thread(){
    public void run(){
        ServerSocketConnection scn = (ServerSocketConnection)
                Connector.open("socket://:1234");
        // Wait for a connection.
        SocketConnection sc = (SocketConnection) scn.acceptAndOpen();
       //do other operation
    }
};
t.start();

Lets say no client is connecting to the Server, so this thread will be blocked.Now I want to kill the above thread t? How can I kill it?

A: 

You can call thread.interrupt(). This will throw a InterruptedException in your thread

Nikolaus Gradwohl
are you sure? acceptAndOpen is not declared to throw InterruptedException
aioobe
this is a runtime Exception that's not generated by acceptAndOpen but by the Thread-class.
Nikolaus Gradwohl
In which API is InterruptedException a runtime exception? You have an URL?
aioobe
A: 

Call Thread.interrupt(). Blocking operations typically throw an InterruptedException if the Thread they are executing on is interrupted.

Tim Bender
acceptAndOpen does not throw InterruptedException.
aioobe
A: 

Make the main Thread MONITOR THREAD (from where you spawn this new Thread) join() for this Thread. If after a given time-out, this thread does not "join" then you can ignore (nullify) this thread. This will give you an added functionality adding time-out for blocking thread.

Tushar Tarkas
+1  A: 

Thread.interrupt() will not interrupt a thread blocked on a socket. You can try to call Thread.stop() or Thread.destroy(), but these methods are deprecated (edit: actually, absent in J2ME) and in some cases non-functional, for reasons you can read about here. As that article mentions, the best solution in your case is to close the socket that you're blocking on:

In some cases, you can use application specific tricks. For example, if a thread is waiting on a known socket, you can close the socket to cause the thread to return immediately. Unfortunately, there really isn't any technique that works in general. It should be noted that in all situations where a waiting thread doesn't respond to Thread.interrupt, it wouldn't respond to Thread.stop either. Such cases include deliberate denial-of-service attacks, and I/O operations for which thread.stop and thread.interrupt do not work properly.

pelotom
Are you sure? In which api are you looking? I can't find Thread.stop() in CLDC or MIDP...
aioobe
@aioobe: J2SE... in any case I'm saying *don't* use stop()
pelotom
ok, cause he's talking about java-me here...
aioobe
I think the main point is still relevant: you have to kill what you're blocking on, you can't just kill the thread directly... I'm guessing the reason stop() doesn't exist in J2ME is because of the reasons given in the article I linked.
pelotom
A: 

In your main thread (when you want to terminate the accept-thread) call scn.close(). The acceptAndOpen should then throw an IOException, which you may catch and then terminate the accept-thread gracefully.

ServerSocketConnection scn =
    (ServerSocketConnection) Connector.open("socket://:1234");

Thread t = new Thread(){
    public void run(){
        // Wait for a connection.
        try {
            SocketConnection sc = (SocketConnection) scn.acceptAndOpen();
            //do other operation
        } catch (IOException e) {
            // Log the exception
        }
    }
};

t.start();

// ...

// Somewhere else...
// Cancel the acceptAndOpen.
scn.close();

A good idea would be to introduce a synchronization mechanism so you don't close the socket right after the acceptAndOpen went through.

aioobe
A: 

I think you would just need to close the connection - so simply make scn a field of the thread and implement:

public void stop() {
  scn.close();
}

and code your main method to exit gracefully on IOException or other indication that you are done (as you probably already have).

Sam Brightman