Grokking The Java Developer Interview: More Than 200 Questions To Crack The Java, Spring, SpringBoot & Hibernate Interview



Download 7,31 Mb.
Pdf ko'rish
bet62/89
Sana21.04.2022
Hajmi7,31 Mb.
#571320
1   ...   58   59   60   61   62   63   64   65   ...   89
Bog'liq
Grokking-The-Java-Developer-Interview-More-Than-200-Questions-To-Crack-The-Java -Spring -SpringBoot-

Scenario 2 : There are 2 threads, one is calling static synchronized
method on one object, and the other thread is calling non-static
synchronized method on another object.


Here, these 2 threads will not block each other and will be executed
concurrently as both locks are different, the thread executing the
static synchronized method holds a lock on the class and the thread
executing the non-static synchronized method holds the lock on the
object on which the method has been called.
In short, static synchronized method do not block a non-static
synchronized method.
Program:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
84\Scenario 2\color1.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
84\Scenario 2\color2.png


(Main class is same as Scenario 1)
Output:
C:\Users\jjatin\Desktop\Different
Versions\All_Photos\Question 84\Scenario
2\output.png
Question 85: What is Callable Interface?


Answer: Callable interface represents an asynchronous task which
can be executed by a separate thread, it has one call() method,
which returns a Future object.
A Runnable can be executed by passing it into the Thread class
constructor but Thread class does not have a constructor that
accepts a Callable, so Callable can be executed only by submit()
method of ExecutorService Interface.
Callable’s call() method can throw checked exceptions, the
exceptions are collected in Future object which can be checked by
making a call to Future.get() method, an ExecutionException is
thrown which wraps the original exception. We can get the original
checked exception by making a call to getCause() method on the
exception object thrown. However, if we don’t call Future.get()
method then the exception will not be reported back and the task will
be marked as completed.
One thing you should remember is that the Future.get() method
blocks the execution, so timeouts should be used when using this
method to avoid unexpected waits.
Program showing how Callable is used and how the result is
returned in Future object:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
85\color1.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
85\color2.png


Output:
C:\User
s\jjatin\De
sktop\Diff
erent
Versions\
All Photo
Let’s pass a negative number, so that exception will be thrown:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
85\color3.png


Output:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
85\error.png


You can call e.getCause() to get the original exception.

Download 7,31 Mb.

Do'stlaringiz bilan baham:
1   ...   58   59   60   61   62   63   64   65   ...   89




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish