•
•
•
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.
Do'stlaringiz bilan baham: