Java中關於單例模式的10個面試問題

單例模式是最常見的一個模式,在Java中單例模式被大量的使用。這同樣也是我在面試時最喜歡提到的一個面試問題,然後在面試者回答後可以進一步挖掘其細節,這不僅檢查了關於單例模式的相關知識,同時也檢查了面試者的編碼水平、多線程方面的知識,這些在實際的工作中非常重要。

在這個簡單的Java面試教程中,我列舉了一些Java面試過程中關於單例模式的常會被提到的問題。關於這些面試問題,我沒有提供答案,因爲你通過百度搜索很容易找到這些答案。

那麼問題就從什麼是單例模式?你之前用過單例模式嗎?開始

單例是在整個應用中只有一個實例的類,然後提供getInstance()方法來訪問該單例實例。

1) 哪些類是單例模式的後續類?在Java中哪些類會成爲單例?

這裏它們將檢查面試者是否有對使用單例模式有足夠的使用經驗。他是否熟悉單例模式的優點和缺點。


2)你能在Java中編寫單例裏的getInstance()的代碼?

很多面試者都在這裏失敗。然而如果不能編寫出這個代碼,那麼後續的很多問題都不能被提及。

3)在getInstance()方法上同步有優勢還是僅同步必要的塊更優優勢?你更喜歡哪個方式?

這確實是一個非常好的問題,我幾乎每次都會提該問題,用於檢查面試者是否會考慮由於鎖定帶來的性能開銷。因爲鎖定僅僅在創建實例時纔有意義,然後其他時候實例僅僅是隻讀訪問的,因此只同步必要的塊的性能更優,並且是更好的選擇。

4)什麼是單例模式的延遲加載或早期加載?你如何實現它?

這是和Java中類加載的載入和性能開銷的理解的又一個非常好的問題。我面試過的大部分面試者對此並不熟悉,但是最好理解這個概念。

5) Java平臺中的單例模式的實例有哪些?

這是個完全開放的問題,如果你瞭解JDK中的單例類,請共享給我。

6) 單例模式的兩次檢查鎖是什麼?

7)你如何阻止使用clone()方法創建單例實例的另一個實例?

該類型問題有時候會通過如何破壞單例或什麼時候Java中的單例模式不是單例來被問及。

8)如果阻止通過使用反射來創建單例類的另一個實例?

開放的問題。在我的理解中,從構造方法中拋出異常可能是一個選項。

9)如果阻止通過使用序列化來創建單例類的另一個實例?

又一個非常好的問題,這需要Java中的序列化知識並需要理解如何使用它來序列化單例類。該問題是開放問題。

10) Java中的單例模式什麼時候是非單例?

下面是英文:

Singleton pattern is one of the most common patterns available and it’s also used heavily in Java. This is also one of my favorite interview question and has lots of interesting follow-up to digg into details , this not only check the knowledge of design pattern but also check coding , multithreading aspect which is very important while working for a real life application.

In this short java interview tutorial I have listed some of the most common question asked on Singleton pattern during a Java Interview. I have not provided the answers of these questions as they are easily available via google search but if you guys need I can try to modify this tutorial to include answers as well.

So question starts with what is Singleton? Have you used Singleton before?
Singleton is a class which has only one instance thought out the application and provides a getInstance() method to access the singleton instance.

1) Which classes are candidates of Singleton? Which kind of class do you make Singleton in Java?
Here they will check whether candidate has enough experience on usage of singleton or not. Does he is familiar of advantage/disadvantage or alternatives available for singleton in Java or not.

2) Can you write code for getInstance() method of a Singleton class in Java?
Most of the guys fail here if they have mugged up the singleton code because you can ask lots of follow-up question based upon the code written by candidate. In my experience I have seen mostly java interviewee right Singleton getInstance() method with double checking but they are not really familiar with the caveat associated with double checking of singleton prior to Java 5.

3) Is it better to make whole getInstance() method synchronized or just critical section is enough? Which one you will prefer?
This is really nice question and I mostly asked to just quickly check whether candidate is aware of performance trade off of unnecessary locking or not. Since locking only make sense when we need to create instance and rest of the time its just read only access so locking of critical section is always better option.

4) What is lazy and early loading of Singleton and how will you implement it?
This is again great question in terms of understanding of concept of loading and cost associated with class loading in Java. Many of which I have interviewed not really familiar with this but its good to know concept.

5) Example of Singleton in standard JAVA Development Kit?
This is open question to all, please share which classes are Singleton in JDK.

6) What is double checked locking in Singleton?
One of the most hyped question on Singleton and really demands complete understanding to get it right because of Java Memory model caveat prior to Java 5. If a guy comes up with a solution of using volatile instance of Singleton then it really shows it has in depth knowledge of Java memory model and he is constantly updating his Java knowledge.

7) How do you prevent for creating another instance of Singleton using clone() method?
This type of questions generally comes some time by asking how to break singleton or when Singleton is not Singleton in Java.

8) How do you prevent for creating another instance of Singleton using reflection?
Open to all. In my opinion throwing exception from constructor is an option.

9) How do you prevent for creating another instance of Singleton during serialization?
Another great question which requires knowledge of Serialization in Java and how to use it for persisting Singleton classes. This is open to you all but in my opinion use of readResolve() method can sort this out for you.

10) When is Singleton not a Singleton in Java?
There is a very good article present in Sun's Java site which discusses various scenarios when a Singleton is not really remains Singleton and multiple instance of Singleton is possible. Here is the link of that article http://java.sun.com/developer/technicalArticles/Programming/singletons/

If you like to read more Java interview questions you can have a look on some of my favorites below:

How to identify and fix deadlock in java
Difference between HashMap and HashTable? Can we make hashmap synchronized
How to check if a thread holds lock on a particular object in Java


From:http://nanjingjiangbiao-t.iteye.com/blog/1794520

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章