Java Multithreading Interview Questions

This is one of the most typical characters in java programming. Java multithreading interview questions are usually asked in job interviews of game development companies. Here is a list of some of the most frequently asked java multithreading interview questions.

1. What is thread?

A thread is a lightweight subprocess. It is a separate path of execution.It is called separate path of execution because each thread runs in a separate stack frame.

2. What about the daemon threads?

The daemon threads are basically the low priority threads that provide the background support to the user threads. It provides services to the user threads.

3. Can we make the user thread as daemon thread if thread is started?

No, if you do so, it will throw IllegalThreadStateException

4. What is synchronization?

Synchronization is the capability to control the access of multiple threads to any shared resource.

It is used:

For preventing thread interference.

For preventing consistency problem.

5. Is it possible to start a thread twice?

No, there is no possibility to start a thread twice.

6. What does join() method do?

The join() method causes the currently running threads to stop executing until the thread it joins with completes its task.

The wait() method releases the lock.

The sleep() method doesn’t release the lock.

7. What is static synchronization?

If you make any static method as synchronized, the lock will be on the class not on object.

8. What is the difference between notify() and notifyAll()?

The notify() is used to unblock one waiting thread whereas notifyAll() method is used to unblock all the threads in waiting state.

9. What is the difference between time slicing and preemptive scheduling?

Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors. Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence.

10. What is the purpose of Synchronized block?

Synchronized block is used to lock an object for any shared resource. Scope of synchronized block is smaller than the method.

11. What is multithreading?

Multithreading is the task of executing multiple threads at the same time. Its main advantages are:

It is lightweight.

It shares the same address space. Cost of communication between process is low.

12. When should we interrupt a thread?

We should interrupt a thread if we want to break out the sleep or wait state of a thread.

13. Can Java object be locked down for exclusive use by a given thread?

Yes. You can lock an object by putting it in a “synchronized” block. The locked object is inaccessible to any thread other than the one that explicitly claimed it.

14. Can we call the run() method instead of start()?

Yes, but it will not work as a thread. Instead, it will work as a normal object so there will not be context-switching between the threads.

print

Leave a Reply

Your email address will not be published. Required fields are marked *