Multithreading Interview Questions in Java for Freshers What are the benefits of using Multithreading? 2
Yüklə
0,82 Mb.
Pdf görüntüsü
səhifə
8/20
tarix
13.12.2023
ölçüsü
0,82 Mb.
#176388
1
...
4
5
6
7
8
9
10
11
...
20
Threads
Bu səhifədəki naviqasiya:
Page 12 © Copyright by Interviewbit Multithreading Interview Questions
Page 13 © Copyright by Interviewbit Multithreading Interview Questions synchronized
9. What’s the difference between notify() and notifyAll() notify()
Multithreading Interview Questions notifyAll()
public void
run
()
{
// Checking whether the thread is Daemon or not
if
(Thread.currentThread().isDaemon())
{
System.out.println(getName() +
" is Daemon thread"
);
}
else
{
System.out.println(getName() +
" is User thread"
);
}
}
public static void
main
(String[] args)
{
DaemonThread t1 =
new
DaemonThread(
"t1"
);
DaemonThread t2 =
new
DaemonThread(
"t2"
);
DaemonThread t3 =
new
DaemonThread(
"t3"
);
// Setting user thread t1 to Daemon
t1.setDaemon(
true
);
// starting first 2 threads
t1.start();
t2.start();
// Setting user thread t3 to Daemon
t3.setDaemon(
true
);
t3.start();
}
}
Output:
Page 12
© Copyright by Interviewbit
Multithreading Interview Questions
t1 is Daemon thread
t3 is Daemon thread
t2 is User thread
But one can only call the
setDaemon()
method before start() method otherwise it
will definitely throw IllegalThreadStateException as shown below:
public class
DaemonThread
extends
Thread
{
public void
run
()
{
System.out.println(
"Thread name: "
+ Thread.currentThread().getName());
System.out.println(
"Check if its DaemonThread: "
+ Thread.currentThread().isDaemon());
}
public static void
main
(String[] args)
{
DaemonThread t1 =
new
DaemonThread();
DaemonThread t2 =
new
DaemonThread();
t1.start();
// Exception as the thread is already started
t1.setDaemon(
true
);
t2.start();
}
}
Output:
Thread name: Thread-0
Check if its DaemonThread: false
8. What are the wait() and sleep() methods?
wait()
: As the name suggests, it is a non-static method that causes the current thread
to wait and go to sleep until some other threads call the notify () or notifyAll()
method for the object’s monitor (lock). It simply releases the lock and is mostly used
for inter-thread communication. It is defined in the object class, and should only be
called from a synchronized context.
Example:
Page 13
© Copyright by Interviewbit
Multithreading Interview Questions
synchronized
(monitor)
{
monitor.wait(); Here Lock Is Released by Current Thread
}
sleep()
: As the name suggests, it is a static method that pauses or stops the execution
of the current thread for some specified period. It doesn’t release the lock while
waiting and is mostly used to introduce pause on execution. It is defined in thread
class, and no need to call from a synchronized context.
Example:
9. What’s the difference between notify() and notifyAll()?
notify()
: It sends a notification and wakes up only a single thread instead of multiple
threads that are waiting on the object’s monitor.
Page 14
© Copyright by Interviewbit
synchronized
(monitor)
{
Thread.sleep(
1000
); Here Lock Is Held by The Current Thread
//after 1000 milliseconds, the current thread will wake up, or after we call that is in
}
Multithreading Interview Questions
notifyAll()
: It sends notifications and wakes up all threads and allows them to
compete for the object's monitor instead of a single thread.
Yüklə
0,82 Mb.
Dostları ilə paylaş:
1
...
4
5
6
7
8
9
10
11
...
20
Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət
gir
|
qeydiyyatdan keç
Ana səhifə
Stomatologiya
Anesteziologiya
Cərrahlıq
Ginekologiya
Tibb
yükləyin