When a virtual machine encounters sleep(long) -statement, it interrupts the current thread. The "current thread" at this point is always a thread called Thread.sleep() . Then he says:
Hey! There is nothing to do in this thread (because I have to wait). I will continue another topic.
Changing the flow is called "to yield". (Note: you can do it yourself by calling Thread.yield(); )
So, no need to figure out what the current thread is. This is always a Thread called sleep (). Note. You can get the current thread by calling Thread.currentThread();
A brief example:
// here it is 0 millis blahblah(); // do some stuff // here it is 2 millis new Thread(new MyRunnable()).start(); // We start an other thread // here it is 2 millis Thread.sleep(1000); // here it is 1002 millis
MyRunnable its run() method:
// here it is 2 millis; because we got started at 2 millis blahblah2(); // Do some other stuff // here it is 25 millis; Thread.sleep(300); // after calling this line the two threads are sleeping... // here it is 325 millis; ... // some stuff // here it is 328 millis; return; // we are done;
Martijn courteaux
source share