1. Thread class sleep() usage
- If a thread don’t want perform any operation for a particular amount of time then we should go for sleep() method.
- Syntax of sleep() method
- Public static native void sleep(long milliseconds);
- Public static void sleep(long milliseconds, int nanoseconds);
- Every sleep() throws InterruptedException, it is checked exception, hence we are using sleep() compulsory we should handle InterruptedExeption either by try catch or throws keyword otherwise we will get compile time error.
- sleep() is static method so we can call this method by using class name.
Thread.sleep(2000L) → Currently executed thread will go to sleep state for 2 secs.
t1.sleep(2000L) → t1 thread will not go to sleep state, this line executed thread will go to sleep state for 2 secs. - Once Thread.sleep(..) will executes than currently executing thread will go to timed waiting state from runnable (running) state.
- After completing time period it will come back to Runnable state (Ready state) from timed waiting state.
- Otherwise within sleeping time only, if any other executing thread interrupts sleeping thread than also it will come back to runnable state(ready state) from timed waiting state.
- So sleep() time completes or sleeping thread is interrupted then it will goes to Runnable state.
- Thread.sleep causes the current thread to suspend execution for a specified period.
- This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.
public static void main(String[] args)throws Exception{
for(int i=0; i<10; i++){
System.out.println(“Slide-”+i);
Thread.currentThread().sleep(2000);
}
}
2. Thread class overloaded sleep() → sleep(long ms) vs sleep(long ms, int ns)
- sleep(long ms) → specifies the sleep time to the milliseconds.
- sleep(long ms, int ns) → specifies the sleep time to the nanoseconds.
- However, these sleep times are not guaranteed to be precise, because they are limited by the facilities provided by the underlying OS.
- Also, the sleep period can be terminated by interrupts.
- sleep(long ms) is a native method so implementation is provided in native language like ‘c’.
- sleep(long ms, int ns) is java method it will convert nanoseconds to equivalent milliseconds and call sleep(long ms) internally.Add the total time (ms + ns)
- If we pass milliseconds value as -ve numbers or nanoseconds value is not in the range of 0-999999 than it throws IllegalArgumentException.
3. sleep(long ms, int ns) → range of ns is 0 to 999999 ?
- 1 millisecond = 10,00000 nanoseconds.
- Native language sleep(long ms) will accept milliseconds only, so in java they are converting nanoseconds to milliseconds like below model
Nanoseconds 1 to 500,000 → 0 milliseconds (mill != 0 otherwise 1 millisecond)
Nanoseconds 500,000 to 10,00,000 → 1 milliseconds - So nanoseconds range is 0 to 999999 (10 lack nanoseconds) → int is taken instead of long.
4. Why some methods are static and some methods are non static in Thread class?
- If some features are applicable to all threads then those features(methods) will make as non static , because we can call by particular thread object.
- If some features are applicable to currently executing thread then those features (methods) will make as static., we can directly call by class name.
Thread.sleep(), Thread.yield()..... → Applicable for currently executing thread
t1.interrupt() → Applicable for t1 thread only
public class ThreadDemo extends Thread{
@Override
public void run() {
for(int i=0; i<10; i++){
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("count is = "+i);
}
}
public static void main(String[] args) throws InterruptedException {
ThreadDemo t1 = new ThreadDemo();
ThreadDemo t2 = new ThreadDemo();
System.out.println(t1.getState());
System.out.println(t2.getState());
t1.start();
t2.start();
System.out.println(t1.getState());
System.out.println(t2.getState());
Thread.sleep(500l);
System.out.println(t1.getState());
System.out.println(t2.getState());
}
}
|
No comments:
Post a Comment