34. Explain thread priority.
Thread priority simply means that threads with the highest priority will get a chance
for execution prior to low-priority threads. One can specify the priority but it's not
necessary that the highest priority thread will get executed before the lower-priority
thread. Thread scheduler assigns processor to thread on the basis of thread priority.
The range of priority changes between 1-10 from lowest priority to highest priority.
Page 28
© Copyright by Interviewbit
Multithreading Interview Questions
35. What do you mean by the ThreadLocal variable in Java?
ThreadLocal variables are special kinds of variables created and provided by the Java
ThreadLocal class. These variables are only allowed to be read and written by the
same thread. Two threads cannot be able to see each other’s ThreadLocal variable,
so even if they will execute the same code, then there won't be any race condition
and the code will be thread-safe.
Example:
Page 29
© Copyright by Interviewbit
Multithreading Interview Questions
public class
ThreadLocalExp
{
public static class
MyRunnable
implements
Runnable
{
private ThreadLocal threadLocal =
new ThreadLocal();
@Override
public void
run
() {
threadLocal.set( (int) (Math.random() *
50D
) );
try
{
Thread.sleep(
1000
);
} catch (InterruptedException e) {
}
System.out.println(threadLocal.get());
}
}
public static void
Dostları ilə paylaş: |