1. Which two method you need to implement for key Object in HashMap ?
In order to use any object as Key in HashMap, it must implements equals and hashcode method in Java.
13. When do you override hashcode and equals() ?
Whenever necessary especially if you want to do equality check or want to use your object as key in HashMap.
14. What will be the problem if you don't override hashcode() method ?
You will not be able to recover your object from hash Map if that is used as key in HashMap.
On the other hand, submit() method is defined in the ExecutorService interface which is a sub-interface of Executor and adds the functionality of terminating the thread pool, along with adding submit() method which can accept a Callable task and return a result of computation.
See CyclicBarrier vs CountDownLatch in Java for more differences.
Read full article from Top 25 Most Frequently Asked Interview Core Java Interview Questions And Answers
In order to use any object as Key in HashMap, it must implements equals and hashcode method in Java.
13. When do you override hashcode and equals() ?
Whenever necessary especially if you want to do equality check or want to use your object as key in HashMap.
14. What will be the problem if you don't override hashcode() method ?
You will not be able to recover your object from hash Map if that is used as key in HashMap.
3. What is the difference between creating String as new() and literal?
When we create string with new() Operator, it’s created in heap and not added into string pool while String created using literal are created in String pool itself which exists in PermGen area of heap.
String s = new String("Test");
does not put the object in String pool , we need to call String.intern() method which is used to put them into String pool explicitly. its only when you create String object as String literal e.g. String s = "Test" Java automatically put that into String pool.
does not put the object in String pool , we need to call String.intern() method which is used to put them into String pool explicitly. its only when you create String object as String literal e.g. String s = "Test" Java automatically put that into String pool.
8. What is difference between Executor.submit() and Executer.execute() method ?
There is a difference when looking at exception handling. If your tasks throws an exception and if it was submitted with execute this exception will go to the uncaught exception handler (when you don't have provided one explicitly, the default one will just print the stack trace to System.err).
If you submitted the task with submit any thrown exception, checked exception or not, is then part of the task's return status. For a task that was submitted with submit and that terminates with an exception, the Future.get will re-throw this exception, wrapped in an ExecutionException.
ExecuterService.submit() can return result of computation because it has a return type of Future, but execute() method cannot return anything because it's return type is void.
1) The submit() can accept both Runnable and Callable task but execute() can only accept the Runnable task.
2) The submit() method is declared in ExecutorService interface while execute() method is declared in the Executor interface.
3) The return type of submit() method is a Future object but return type of execute() method is void.
2) The submit() method is declared in ExecutorService interface while execute() method is declared in the Executor interface.
3) The return type of submit() method is a Future object but return type of execute() method is void.
The difference is that
execute
doesn't return a Future
, so you can't wait for the completion of the Runnable
and get any exception it throws using that.
The difference is that
execute
doesn't return a Future
, so you can't wait for the completion of the Runnable
and get any exception it throws using that.
The
submit(...)
method is an executor framework extension introduced in ExecutorService
interface.
Its main difference from
execute(Runnable)
is that submit(...)
can accept a Callable<V>
(whereas execute()
accepts only Runnable
) and returns an instance of Future<V>
, which you can use later in the caller to retrieve the result asynchronously (potentially blocking until the computation performed by the Callable
is completed).
As you see from the JavaDoc
execute(Runnable)
does not return anything.
However,
submit(Callable<T>)
returns a Future
object which allows a way for you to programatically cancel the running thread later as well as get the T
that is returned when the Callable
completes. See JavaDoc of Future for more detailsFuture<?> future = executor.submit(longRunningJob);
...
//long running job is taking too long
future.cancel(true);
Moreover, if
future.get() == null
and doesn't throw any exception then Runnable executed successfully
The difference is that
execute
simply starts the task without any further ado, whereas submit
returns a Future
object to manage the task. You can do the following things with the Future
object:- Cancel the task prematurely, with the
cancel
method. - Wait for the task to finish executing, with
get
.
The
Future
interface is more useful if you submit a Callable
to the pool. The return value of the call
method will be returned when you call Future.get
. If you don't maintain a reference to the Future
, there is no difference.execute:
Use it for fire and forget callssubmit:
Use it to inspect the result of method call and take appropriate action on Future
objected returned by the call
Major difference:
Exception
handlingsubmit()
hides un-handled Exception
in framework itself.execute()
throws un-handled Exception
.
Solution for handling Exceptions with
submit()
- Wrap your
Callable or Runnable code in try{} catch{} block
OR - Keep
future.get() call in try{} catch{} block
OR - implement your own
ThreadPoolExecutor
and overrideafterExecute
method
A task queued with
execute()
that generates some Throwable
will cause the UncaughtExceptionHandler
for the Thread
running the task to be invoked. The default UncaughtExceptionHandler
, which typically prints the Throwable
stack trace to System.err
, will be invoked if no custom handler has been installed.
On the other hand, a
Throwable
generated by a task queued with submit()
will bind the Throwable
to the Future
that was produced from the call to submit()
. Calling get()
on that Future
will throw an ExecutionException
with the original Throwable
as its cause (accessible by calling getCause()
on the ExecutionException
).
submit method has 3 forms >
<T> Future<T> submit(Callable<T> task)
Submits a callable task for execution.
Method returns a Future which represents pending results of the task.
Once task is completed Future's get method will return the task's result.
<T> Future<T> submit(Runnable task, T result)
Submits a Runnable task for execution.
Method returns a Future which represents that task. Once task is completed Future's get method will return result.
Future<?> submit(Runnable task)
Submits a Runnable task for execution.
Method returns a Future which represents that task. Once task is completed Future's get method will return null.
24. What is difference between CyclicBarrier and CountDownLatch in Java
Main difference between both of them is that
you can reuse CyclicBarrier even if Barrier is broken but you can not reuse CountDownLatch in Java. See CyclicBarrier vs CountDownLatch in Java for more differences.