More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. The goal of a synchronised block is to achieve mutual exclusion i.e at one time, the segment of the code should be executed by single thread only and hence the lock needs to be retrieved before executing the segment and then released.
Help us improve. Please let us know the company, where you were asked this question :
Ans. 1. Switching Overheads - Even though multi threading aims at improving performance by reducing the wait time and hence improving overall throughput, there is a cost of switching resources between threads and sometime this cost can surpass the benefits if there isnt much wait for IO or external communication.
2. Debugging is hard with multi threaded code.
3. Deadlock - Execution of multi threaded code many a times lead to deadlock due to shared resources.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Synchronize is used to achieve mutual exclusion i.e at one time, the segment of the code, method that has been declared synchronized should be executed by single thread only and hence the lock needs to be retrieved before executing the segment and then released.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Games are very good examples of threading.You can use multiple objects in games like cars, motor bikes, animals, people etc. All these objects are nothing but just threads that run your game application.
Help us improve. Please let us know the company, where you were asked this question :
Q51. Suppose we make an api call using multithreading, How would you design to make sure that we have received response for all threads before exiting the main thread ?
Ans. We can use HashMap for tracking response status for all threads. We can wait every n second by using Thread.sleep and exit the main thread only once we have received response for all threads.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Every process in it's timeline require different resources. Utilization of resources can be optimized when they are shared among different processes or threads. When one thread is sleeping waiting for a peripheral to complete (e.g. a disk write, or a key press from the keyboard), other threads can continue using processor time and hence would lead to better usage of resources.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Context Switching is the process of storing and restoring of CPU state so that Thread execution can be resumed from the same point at a later point of time.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Java provides its own implementations of the thread pool pattern, through objects called executors. These can be used through executor interfaces or directly through thread pool implementations which does allow for finer-grained control. The java.util.concurrent package contains the following interfaces:
Executor : a simple interface for executing tasks.ExecutorService a more complex interface which contains additional methods for managing the tasks and the executor itself.
ScheduledExecutorService: extends ExecutorService with methods for scheduling the execution of a task.Alongside these interfaces, the package also provides the Executors helper class for obtaining executor instances, as well as implementations for these interfaces.
Generally, a Java thread pool is composed of:
The pool of worker threads, responsible for managing the threads.
A thread factory that is responsible for creating new threads.
A queue of tasks waiting to be executed.
Help us improve. Please let us know the company, where you were asked this question :