1

I am creating an program and working with threads in details for the first time and stuck into an situation .Please help me in that. I am having a thread which is in wait state.Now at some instance I want to kill or to awake thread and resume from another class .For this I am saving object of thread .I don't know how to do this .I tried to notify thread but got exception.Here is my code:

Class one:

Thread t= new Thread(new Runnable() {      
    @Override
    public void run() {
        try{  
            Thread.sleep(VariableClass.THREAD_WAIT_SECONDS);
            if(message !=null)
            message_status = message.getStatus();
        }
        catch(InterruptedException e)
        {
            e.printStackTrace();
        }
        //do other stuff and save the thread object
        VariableClass.threads.remove(message.getUniqueId());
    }
});
t.start();
VariableClass.threads.put(pojo.getUniqueId(),t);

Class two:

Thread t =VariableClass.threads.get(tempId);
t.notify();

I just want to resume or kill thread.

Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
Sneha Bansal
  • 941
  • 1
  • 13
  • 38
  • 1
    i guess you should use `t.interrupt()` – SpaceTrucker May 28 '15 at 13:32
  • 1
    This may be an instance of the XY problem (http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) What is your real goal? If you want to pass a message from one thread to another, then maybe you ought to be using some kind of a queue (e.g., `java.util.concurrent.SynchronousQueue` or `java.util.concurrent.BlockingQueue). – Solomon Slow May 28 '15 at 16:00

3 Answers3

1

If your thread t is sleeping, calling t.interrupt() will cause an InterruptedException to be thrown from the line calling Thread#sleep. It will get caught in your catch block and your thread will proceed from there to do its cleanup and exit.

If there was an issue where your thread was not sleeping or waiting but still wanted to be aware of whether it was interrupted, the code in your Runnable could check the interrupted flag on the current thread. Remember that the interrupted flag gets reset once an InterruptedException is thrown.

Wait and notify are for threads that are synchronizing on a monitor, that's not applicable to your example. Threads wait on a monitor and receive notifications, but the notifications are not made to a specific thread; for Object#notify, some thread waiting on that monitor gets chosen but the thread calling notify has no control over which one is picked.

Here's an example of using interrupt to wake a thread from sleeping.

Community
  • 1
  • 1
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
0

Your thread is sleeping for the specified amount of time. Call interrupt on it, if you just want to "kill it" and you don't care too much what will happen with it later. You cannot simply "awake it" from another thread, if it's sleeping it has to sleep as much as it has been told to. Calling notify has nothing to do with this situation (there's no prior wait call). Even if did, you're calling it incorrectly.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
0

You do not use notify in this case. I suggest reading the JavaDoc on #wait/#notify/#notifyAll

You use #notify and #notifyAll to create a framework with concurrency such as a Thread that does work on an instance of a certain object and other threads are waiting to work on it.

A thread "dies" out if the run function is over, but if you want to stop the thread immediately, use #interrupt.