Java Multithreading Interview Questions

Source: Internet
Author: User
Tags thread class java collections interview questions

Java Multithreading interview Questions

Multithreading and synchronization are considered as the typical chapter in Java programming. In game development company, Mulithreading related interview questions is asked mostly. A List of frequently asked Java Multithreading interview questions is given below.

1) What is multithreading?

Multithreading is a process of executing multiple threads simultaneously. Its main advantage is:

    • Threads share the same address space.
    • Thread is lightweight.
    • Cost of communication between process was low.

More details ...

2) What is thread?

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

More details ...

3) What is the difference between preemptive scheduling and time slicing?

Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher PRI Ority task comes into existence. 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 precedence and other factors.

4) What does join () method?

The join () method waits for a thread to die. In other words, it causes the currently running threads-stop executing until the thread it joins with completes it TAS K.

More details ...

5) What is difference between wait () and sleep () method?
Wait () sleep ()
1) The Wait () method is defined in Object class. The sleep () method is defined in Thread class.
2) Wait () method releases the lock. The sleep () method doesn ' t releases the lock.

6) Is it possible to start a thread twice?

No, there is no possibility to start a thread twice. If we does, it throws an exception.

More details ...

7) Can We call the run () method instead of start ()?

Yes, but it won't work as a thread rather it would work as a normal object so there won't be context-switching betwee n the threads.

More details ...

8) What is about the daemon threads?

The daemon threads is basically the low priority threads that provides the background support to the user threads. IT provides services to the user threads.

More details ...

9) Can we make the user thread as daemon thread if thread is started?

No, if you do so, it'll throw illegalthreadstateexception

More details ...

10) What is shutdown hook?

The shutdown hook is basically a thread i.e. invoked implicitely before JVM shuts down. So we can use it perform the clean up resource.

More details ...

11) 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.

More details ...

What is synchronization?

Synchronization is the capabilility of control, the access of multiple threads to any shared resource. It is used:

    1. To prevent thread interference.
    2. To prevent consistency problem.

More details ...

What is the purpose of Synchronized block?
    • Synchronized block is used to lock a object for any shared resource.
    • Scope of synchronized block is smaller than the method.

More details ...

14) Can Java object is locked down for exclusive use by a given thread?

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

What is a static synchronization?

If you do any of the static method as synchronized, the lock'll be on the class is not on object. More details ...

16) What is the difference between notify () and Notifyall ()?

The Notify () is used to unblock one waiting thread whereas Notifyall () method are used to unblock all the threads in Waitin G State.

17) What is deadlock?

Deadlock is a situation when the threads is waiting on each other to release a resource. Each thread waiting for a resource which are held by the other waiting thread.

More details ...

Next TopicJava Collections interview Questions

Java Multithreading Interview Questions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.