由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - gg面试题
相关主题
Google电面被拒,郁闷中Bloomberg的电面 希望对你有用兼攒rp
请问一道special singleton class的题早上的面试题 -
Can a 10-year-Java guy answer these 2 questions promptly?关于java synchronized statement和static method or variable
关于singleton 的面试题Java 面试题
Bloomberg, Microsoft, Indeed, ebay面试题Linked电面分享,挺好的题 应该已挂
singleton pattern problem分享面试题
问一个thread safe singleton的问题求一个面试题解答。。
请教个面试题google 面试题疑问
相关话题的讨论汇总
话题: instance话题: integer话题: private
进入JobHunting版参与讨论
1 (共1页)
P********l
发帖数: 452
1
死在这道题上:写一个Singleton.
教训:
不熟/不确定就提也不要提,忍着。
如果你对如下代码有兴趣,看看你能不能该对?
see also: http://www.mitbbs.com/article_t/JobHunting/31595909.html
public final static class SpecialSingleton {
private final Integer i;
private static ConcurrentHashMap
instanceMap = new ConcurrentHashMap(); //use
the concurrent to improve performance.
private SpecialSingleton(Integer i) //it is correct to make the
constructor private.
{
t
Z*****Z
发帖数: 723
2
首先一个小问题,class如果不是inner的,不能是static的把?

the

【在 P********l 的大作中提到】
: 死在这道题上:写一个Singleton.
: 教训:
: 不熟/不确定就提也不要提,忍着。
: 如果你对如下代码有兴趣,看看你能不能该对?
: see also: http://www.mitbbs.com/article_t/JobHunting/31595909.html
: public final static class SpecialSingleton {
: private final Integer i;
: private static ConcurrentHashMap
: instanceMap = new ConcurrentHashMap(); //use
: the concurrent to improve performance.

Z*****Z
发帖数: 723
3
如果getInstance(5)被从两处同时调用,的确会产生2个instance,但是好像只有一个
被放到map里并且返回了。所以function上是OK的把?

the concurrent to improve performance.

【在 P********l 的大作中提到】
: 死在这道题上:写一个Singleton.
: 教训:
: 不熟/不确定就提也不要提,忍着。
: 如果你对如下代码有兴趣,看看你能不能该对?
: see also: http://www.mitbbs.com/article_t/JobHunting/31595909.html
: public final static class SpecialSingleton {
: private final Integer i;
: private static ConcurrentHashMap
: instanceMap = new ConcurrentHashMap(); //use
: the concurrent to improve performance.

Z*****Z
发帖数: 723
4
如果原题是你链接里的话,那么你的code就有问题啊。
还是把getInstance方法给synchronize了吧

【在 P********l 的大作中提到】
: 死在这道题上:写一个Singleton.
: 教训:
: 不熟/不确定就提也不要提,忍着。
: 如果你对如下代码有兴趣,看看你能不能该对?
: see also: http://www.mitbbs.com/article_t/JobHunting/31595909.html
: public final static class SpecialSingleton {
: private final Integer i;
: private static ConcurrentHashMap
: instanceMap = new ConcurrentHashMap(); //use
: the concurrent to improve performance.

P********l
发帖数: 452
5
我提到过用synchronized getinstance。但是,对我来说更安全的回答是不要提同步。
我想提高performance,结果在现场试了其他办法,被人家追的无处藏身。

【在 Z*****Z 的大作中提到】
: 如果原题是你链接里的话,那么你的code就有问题啊。
: 还是把getInstance方法给synchronize了吧

Z*****Z
发帖数: 723
6
同步是必须的吧。像你帖子里的那种写法,只是map自己被synchronize了还不够。
另外,concurrent programming很tricky,现场发挥一些想法的话ms很危险。

【在 P********l 的大作中提到】
: 我提到过用synchronized getinstance。但是,对我来说更安全的回答是不要提同步。
: 我想提高performance,结果在现场试了其他办法,被人家追的无处藏身。

g*******y
发帖数: 1930
7
看题目,像是某mm征gg,出的面试题...
g**e
发帖数: 6127
8
写一个,抛砖引玉。应用于java 5以上
public class SpecialSingleton {
private final int i;
private volatile static SpecialSingleton instance = null;
private static ConcurrentHashMap
instanceMap = new ConcurrentHashMap();
private SpecialSingleton(int i)
{
this.i = i;
}
public static SpecialSingleton getInstance(int i)
{
instance = instanceMap.get(i);


【在 P********l 的大作中提到】
: 死在这道题上:写一个Singleton.
: 教训:
: 不熟/不确定就提也不要提,忍着。
: 如果你对如下代码有兴趣,看看你能不能该对?
: see also: http://www.mitbbs.com/article_t/JobHunting/31595909.html
: public final static class SpecialSingleton {
: private final Integer i;
: private static ConcurrentHashMap
: instanceMap = new ConcurrentHashMap(); //use
: the concurrent to improve performance.

h******3
发帖数: 351
9
public static SpecialSingleton getInstance(int i)
{
instance = instanceMap.get(i);
if (instance == null) {
//to avoid multithreading corruption
synchronized (SpecialSingleton.Class){
if (instance == null) {
instance = new SpecialSingleton(i);
instanceMap.put(i, instance);
g**e
发帖数: 6127
10
跟我写的一样,不过这里instance要是volatile的。好虫在java版给了另外一个方法
instance = instanceMap.get(i);
if (instance == null) {
instance = new SpecialSingleton(i);
temp = instanceMap.putIfAbsent(i, instance);
if(temp != null) return temp;
}
return instance;

【在 h******3 的大作中提到】
: public static SpecialSingleton getInstance(int i)
: {
: instance = instanceMap.get(i);
: if (instance == null) {
: //to avoid multithreading corruption
: synchronized (SpecialSingleton.Class){
: if (instance == null) {
: instance = new SpecialSingleton(i);
: instanceMap.put(i, instance);
:

相关主题
singleton pattern problemBloomberg的电面 希望对你有用兼攒rp
问一个thread safe singleton的问题早上的面试题 -
请教个面试题关于java synchronized statement和static method or variable
进入JobHunting版参与讨论
h******3
发帖数: 351
11
right. instance should be volatile, & only works under Java 5.

【在 g**e 的大作中提到】
: 跟我写的一样,不过这里instance要是volatile的。好虫在java版给了另外一个方法
: instance = instanceMap.get(i);
: if (instance == null) {
: instance = new SpecialSingleton(i);
: temp = instanceMap.putIfAbsent(i, instance);
: if(temp != null) return temp;
: }
: return instance;

Z*****Z
发帖数: 723
12
r u sure? 我怎么觉得这几个版本都有问题

【在 g**e 的大作中提到】
: 跟我写的一样,不过这里instance要是volatile的。好虫在java版给了另外一个方法
: instance = instanceMap.get(i);
: if (instance == null) {
: instance = new SpecialSingleton(i);
: temp = instanceMap.putIfAbsent(i, instance);
: if(temp != null) return temp;
: }
: return instance;

g**e
发帖数: 6127
13
yes。
goodbug的那个效率更高。但是有可能new SpecialSingleton()会被执行几次。如果这
个操作比较耗时,可以用Future

【在 Z*****Z 的大作中提到】
: r u sure? 我怎么觉得这几个版本都有问题
g**e
发帖数: 6127
14
是after java 5吧,之前的版本volatile有问题

【在 h******3 的大作中提到】
: right. instance should be volatile, & only works under Java 5.
j**l
发帖数: 2911
15
是gg碰到的面试题?
我开始还以为是Google面试题,不过他们不怎么问Java吧。

【在 g*******y 的大作中提到】
: 看题目,像是某mm征gg,出的面试题...
Z*****Z
发帖数: 723
16
比如说你自己写的这个版本,某一时刻HashMap里面装着int为1的singleton,
这个时候getInstance(2)被调用,instance被置为null,
这个时候getInstance(1)被调用,instance被置为Map里的那个singleton,
这个时候如果getInstance(2)继续执行下面的if语句,逻辑就不对了吧?

【在 g**e 的大作中提到】
: 写一个,抛砖引玉。应用于java 5以上
: public class SpecialSingleton {
: private final int i;
: private volatile static SpecialSingleton instance = null;
: private static ConcurrentHashMap
: instanceMap = new ConcurrentHashMap();
: private SpecialSingleton(int i)
: {
: this.i = i;
: }

g**e
发帖数: 6127
17
按顺序执行没有问题,因为最开始就做了instance=instanceMap.get(i)
我想了一下,多线程的时候头两行可能有问题
instance = instanceMap.get(i);
if (instance == null) {
有可能Thread A取i, Thread B取j,假设A先进去创建了instance,这时候B才检查
instance == null,会直接返回A创建的instance。

【在 Z*****Z 的大作中提到】
: 比如说你自己写的这个版本,某一时刻HashMap里面装着int为1的singleton,
: 这个时候getInstance(2)被调用,instance被置为null,
: 这个时候getInstance(1)被调用,instance被置为Map里的那个singleton,
: 这个时候如果getInstance(2)继续执行下面的if语句,逻辑就不对了吧?

Z*****Z
发帖数: 723
18
对,我大概就是这个意思。。。

【在 g**e 的大作中提到】
: 按顺序执行没有问题,因为最开始就做了instance=instanceMap.get(i)
: 我想了一下,多线程的时候头两行可能有问题
: instance = instanceMap.get(i);
: if (instance == null) {
: 有可能Thread A取i, Thread B取j,假设A先进去创建了instance,这时候B才检查
: instance == null,会直接返回A创建的instance。

P********l
发帖数: 452
19
我水平有限,在短时间里全面复习也来不及。回头再思考这道题的时候,觉得可以在回
答技巧上改进。我看过Java concurrency in practice前几章,后面的几章没来得及看
。(实际上考官提到了后几章的内容)
我觉得这样回答也许机会更大一些。
1. private ctor. (explain why)
2. static public getInstance(why? note that by convention, the name is
exactly "gentInstance")
3. auto boxing the Integer.
4. the class is defined as final (why?)
5. Map = new HashMap << simple hashmap.
重点是把自己有把握的全亮出来,但是没有同步。
如果问到同步,先解释自己对同步不熟。解释如果class没有escape的话,还是thread
safe的。加synchronized method。
如果对方穷追不舍,在用
Z*****Z
发帖数: 723
20
前天在网上看了个这个,看起来还挺有道理的。说得是做singleton的时候连clone方法
也要重写:
public Object clone()
throws CloneNotSupportedException
{
throw new CloneNotSupportedException();
// that'll teach 'em
}

【在 P********l 的大作中提到】
: 我水平有限,在短时间里全面复习也来不及。回头再思考这道题的时候,觉得可以在回
: 答技巧上改进。我看过Java concurrency in practice前几章,后面的几章没来得及看
: 。(实际上考官提到了后几章的内容)
: 我觉得这样回答也许机会更大一些。
: 1. private ctor. (explain why)
: 2. static public getInstance(why? note that by convention, the name is
: exactly "gentInstance")
: 3. auto boxing the Integer.
: 4. the class is defined as final (why?)
: 5. Map = new HashMap << simple hashmap.

1 (共1页)
进入JobHunting版参与讨论
相关主题
google 面试题疑问Bloomberg, Microsoft, Indeed, ebay面试题
有些面试题是够扯蛋的singleton pattern problem
菜鸟向大家请教个面试题问一个thread safe singleton的问题
面试题求解:remove first duplicate number from an array请教个面试题
Google电面被拒,郁闷中Bloomberg的电面 希望对你有用兼攒rp
请问一道special singleton class的题早上的面试题 -
Can a 10-year-Java guy answer these 2 questions promptly?关于java synchronized statement和static method or variable
关于singleton 的面试题Java 面试题
相关话题的讨论汇总
话题: instance话题: integer话题: private