Core java interview Questions You'll Most Likely Be Asked (Second Edition)


What are the advantages of the new HttpClient introduced


Download 1.52 Mb.
Pdf ko'rish
bet110/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   106   107   108   109   110   111   112   113   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

What are the advantages of the new HttpClient introduced
by Java 9?
Answer:
Java 9 has introduced a new HttpClient class that can be
used to make an Http request through Java code. Prior to Java 9,
the UrlConnection and the HttpUrlConnection classes
could be used to make an HTTP request. The new Java
HttpClient has several advantages over these classes as
follows:





The HttpUrlConnection only supports HTTP 1.1 which is
a very old version of HTTP. HttpClient supports Http 2.0
which is the latest version of HTTP.
Code written via the HttpClient is much cleaner compared
to the code written via HttpUrlConnection.
HttpClient supports asynchronous processing via
CompletableFutures. So, it has a method called
sendAsync that sends an HTTP request asynchronously. It
returns a CompletableFuture which can be used to obtain
the response once the request is completed.



281.
282.
CHAPTER
04
Scenario Based Questions
Suppose you create a new thread by extending the Thread
class and you invoke the start method multiple times.
What do you think will happen?
Answer:
When you invoke the start() method the first time, a new
Thread will be spawned and the code specified within the run()
method will get executed in the body of the thread. However, on
subsequent invocation of the start method, an
IllegalThreadStateException will occur. This is because
you cannot start the same thread more than once. Threads follow
a life cycle and have certain states during the life–cycle. When a
thread is created the first time, it is in the NEW state. After the
start() method is invoked it enters the RUNNABLE state and
once the run method completed, it enters the TERMINATED
state. So, when you invoke the start method on a running thread,
it is already in the RUNNABLE state. This causes the
IllegalThreadStateException. If you want to execute the
code in the thread again, you need to create a new Thread.

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   106   107   108   109   110   111   112   113   ...   171




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