0

I have a main Mina handler thread is processing and in that thread i made another thread and set it to sleep for specified time. Now i want that this inner thread sleep independently without blocking Handler thread. following is sample code.

public void messageReceived(IoSession session, Object message) throws Exception {
        Integer tts = 5000; 
        Thread sleepThread = new Thread(obj);
        sleepThread.sleep(tts);
}

currently it is blocking main Handler thread.

Rizstien
  • 802
  • 1
  • 8
  • 23
  • possible duplicate of [How to make another thread sleep in Java](http://stackoverflow.com/questions/1508278/how-to-make-another-thread-sleep-in-java) – Duncan Jones Apr 23 '13 at 12:11

2 Answers2

2

Thread.sleep() is a static method, so calling sleepThread.sleep(tts) is the same as Thread.sleep(tts). Hence your current thread is just sleeping.

You can't cause another thread to sleep by calling a method on its Thread object. At a push, you could set a flag on the object and your thread could check for the presence of that flag and behave accordingly.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
2

try

    final int tts = 5000; 
    Thread sleepThread = new Thread() {
        public void run() {
            try {
                Thread.sleep(tts);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    };
    sleepThread.start();
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275