09.difference Between Thread
09.difference Between Thread
Vishnu
yield():
Causes the currently executing thread object to temporarily pause for giving a chance to the
remaining waiting threads of the same priority to execute. If no such thread than same thread
will continue its execution.
Syntax: public static void yield().
Example:
ThreadYieldExample.java
package com.sst.threads;
// start threads.
thrd1.start();
thrd2.start();
thrd3.start();
}
}
Output:
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
sleep(long millis):
Causes the currently executing thread to sleep for the specified time as per our requirement.
Syntax: public static void sleep(long millis) .
Example:
ThreadSleepExample.java
package com.sst.threads;
// start threads.
thrd1.start();
thrd2.start();
thrd3.start();
}
}
Output:
1
1
1
2
2
2
3
3
3
4
4
4
5
5
5