Java Multithreading Interview Questions

來源:互聯網
上載者:User

標籤:method   ide   develop   pac   multi   ase   min   roc   sid   

Java Multithreading Interview Questions

Multithreading and Synchronization is considered as the typical chapter in java programming. In game development company, mulithreading related interview questions are asked mostly. A list of frequently asked java multithreading interview questions are 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 is 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 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 priority 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 priority 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 to stop executing until the thread it joins with completes its task.

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 will not work as a thread rather it will work as a normal object so there will not be context-switching between the threads.

more details...

8) What about the daemon threads?

The daemon threads are 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 will 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 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...

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

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

more details...

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

15) What is static synchronization?

If you make any static method as synchronized, the lock will be on the class 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 is used to unblock all the threads in waiting state.

17)What is deadlock?

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

more details...

Next TopicJava Collections interview Questions

Java Multithreading Interview Questions

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.