由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 昨天面试的一道题
相关主题
Apply lock on a class.synchronized method does lock the object that passed into the method as a parameter?
Java练习题 10请问hibernate这个功能如何实现?
Talk a little more about How to lock a filejava ,wait ,notify notifyall
请教一个多线程lock机制的问题CC150 16.6答案是不是有问题? (转载)
Java concurrency的疑惑,难道我理解错了? (转载)Java synchronized method一问
同一个Lock锁两次,性能比较差发现 synchronized 的一个问题
[合集] Java interview question, Thanks.synchronization for counters
interview question:synchronization 锁住了什么?
相关话题的讨论汇总
话题: lock话题: num话题: swap话题: public
进入Java版参与讨论
1 (共1页)
g**e
发帖数: 6127
1
class Num {
private Integer i = 0;
public synchronized void set(int i) {
this.i = i;
}
public synchronized int get() {
return i;
}
public synchronized void swap(Num other) {
//implement a thread safe swap method here.
}
}
g*****g
发帖数: 34805
2
ahh, I don't know how to do this. Usually you either
compare the value of them and lock all of them in the
same order, or you can use ReentrantLock and do tryLock
until you can lock all of them. In swap, you already locks
one firmly and you call other.get, you deadlock already.
It seems you can only do
synchronized(Num.class) inside, and performance will suffer.
Edit: no, even that doesn't solve the problem. I don't know
a solution. I think once both instances get their own lock, there's
no solu

【在 g**e 的大作中提到】
: class Num {
: private Integer i = 0;
: public synchronized void set(int i) {
: this.i = i;
: }
: public synchronized int get() {
: return i;
: }
: public synchronized void swap(Num other) {
: //implement a thread safe swap method here.

g**e
发帖数: 6127
3
原来大牛也不会,我死而无憾了

【在 g*****g 的大作中提到】
: ahh, I don't know how to do this. Usually you either
: compare the value of them and lock all of them in the
: same order, or you can use ReentrantLock and do tryLock
: until you can lock all of them. In swap, you already locks
: one firmly and you call other.get, you deadlock already.
: It seems you can only do
: synchronized(Num.class) inside, and performance will suffer.
: Edit: no, even that doesn't solve the problem. I don't know
: a solution. I think once both instances get their own lock, there's
: no solu

h**j
发帖数: 2033
4
不考虑null或者同一个object
synchronized(other) {
Integer t = this.i;
this.i = other.i;
other.i = t;
}
行不?

【在 g**e 的大作中提到】
: class Num {
: private Integer i = 0;
: public synchronized void set(int i) {
: this.i = i;
: }
: public synchronized int get() {
: return i;
: }
: public synchronized void swap(Num other) {
: //implement a thread safe swap method here.

g*****g
发帖数: 34805
5
No, let's say you have instance a and b.
if a.swap(b) and b.swap(a) are called at the same time,
sync(b) and sync(a) will block for ever waiting each other.
So you have to count on external logic to prevent deadlock.

【在 h**j 的大作中提到】
: 不考虑null或者同一个object
: synchronized(other) {
: Integer t = this.i;
: this.i = other.i;
: other.i = t;
: }
: 行不?

h**j
发帖数: 2033
6
那放一个synchronized(Number.class)在外面?

【在 g*****g 的大作中提到】
: No, let's say you have instance a and b.
: if a.swap(b) and b.swap(a) are called at the same time,
: sync(b) and sync(a) will block for ever waiting each other.
: So you have to count on external logic to prevent deadlock.

h**j
发帖数: 2033
7
还是不行

【在 h**j 的大作中提到】
: 那放一个synchronized(Number.class)在外面?
h*****0
发帖数: 4889
8
死锁吧……

【在 h**j 的大作中提到】
: 不考虑null或者同一个object
: synchronized(other) {
: Integer t = this.i;
: this.i = other.i;
: other.i = t;
: }
: 行不?

g**e
发帖数: 6127
9
好虫其实前面说的很对,我当时给的solution基本上跟哲学家吃饭那一套一样,只有同
时获得两个对象的锁才进行swap,不然立即wait放弃当前获得的lock并notifyAll。但
是面试的人说这样应该work,但不是他想要的答案...

【在 g*****g 的大作中提到】
: No, let's say you have instance a and b.
: if a.swap(b) and b.swap(a) are called at the same time,
: sync(b) and sync(a) will block for ever waiting each other.
: So you have to count on external logic to prevent deadlock.

c*****t
发帖数: 1879
10
Hint:
public synchronized void swap(Num other)
{
synchronized (Num.class)
{
....
}
}

【在 g**e 的大作中提到】
: class Num {
: private Integer i = 0;
: public synchronized void set(int i) {
: this.i = i;
: }
: public synchronized int get() {
: return i;
: }
: public synchronized void swap(Num other) {
: //implement a thread safe swap method here.

相关主题
同一个Lock锁两次,性能比较差synchronized method does lock the object that passed into the method as a parameter?
[合集] Java interview question, Thanks.请问hibernate这个功能如何实现?
interview question:java ,wait ,notify notifyall
进入Java版参与讨论
h*****0
发帖数: 4889
11
再具体点?

【在 c*****t 的大作中提到】
: Hint:
: public synchronized void swap(Num other)
: {
: synchronized (Num.class)
: {
: ....
: }
: }

c*****t
发帖数: 1879
12
Hint #2:
The synchronized functions are meant to confuse you. The problem is
equivalent without having synchronized on these functions.
You need a single lock to lock the critical section. Locking on
Num.class will create a lot of contentions, but avoids deadlock.
If can define other instance and class variables, a better solution
is to define a sequential ordering of objects using static counter and
and id. Another possible approach is to use the HashCode ordering
(assuming this function is

【在 h*****0 的大作中提到】
: 再具体点?
h*****0
发帖数: 4889
13
i'm totally lost...
in this problem it seems there is no way to prevent deadlock and at the same
time achieve atomic method.

【在 c*****t 的大作中提到】
: Hint #2:
: The synchronized functions are meant to confuse you. The problem is
: equivalent without having synchronized on these functions.
: You need a single lock to lock the critical section. Locking on
: Num.class will create a lot of contentions, but avoids deadlock.
: If can define other instance and class variables, a better solution
: is to define a sequential ordering of objects using static counter and
: and id. Another possible approach is to use the HashCode ordering
: (assuming this function is

g*****g
发帖数: 34805
14
The thing is, once you get the lock, you can't release it.
You can get an extra lock on the class object but the instance
lock is not released.

【在 c*****t 的大作中提到】
: Hint #2:
: The synchronized functions are meant to confuse you. The problem is
: equivalent without having synchronized on these functions.
: You need a single lock to lock the critical section. Locking on
: Num.class will create a lot of contentions, but avoids deadlock.
: If can define other instance and class variables, a better solution
: is to define a sequential ordering of objects using static counter and
: and id. Another possible approach is to use the HashCode ordering
: (assuming this function is

k***r
发帖数: 4260
15
May the actor model prevail, to same the brain sells, hehe
d****t
发帖数: 6
16
would two-phase locking help?
g*****g
发帖数: 34805
17
This is a typical deadlock situation, and sun tutorial actually
gives a good example of how to resolve it using Lock.tryLock
above 1.5. For this particular question, it's dead end.

【在 d****t 的大作中提到】
: would two-phase locking help?
c*****t
发帖数: 1879
18

My mistake, but you can release the lock through wait () function.
static Lock glock = ...
public void synchronized swap (Num other)
{
while (true)
{
if (glock.tryLock ())
{
synchronized (other)
{
//...
}
glock.unlock ();
return;
}
else
{
wait (1); // someone is trying to lock glock, must be
// doing swapping. Release the current monitor
// through wait so the other guy could p

【在 g*****g 的大作中提到】
: The thing is, once you get the lock, you can't release it.
: You can get an extra lock on the class object but the instance
: lock is not released.

m******t
发帖数: 2416
19
java.util.concurrent.Exchanger?

【在 g**e 的大作中提到】
: class Num {
: private Integer i = 0;
: public synchronized void set(int i) {
: this.i = i;
: }
: public synchronized int get() {
: return i;
: }
: public synchronized void swap(Num other) {
: //implement a thread safe swap method here.

F****n
发帖数: 3271
20
class Num {
private final Comparable lock; // define an ordered object as lock;
public synchronized void swap(Num other) {
Object firstLock = null;
Object secondLock = null;
if (this.lock.comparesTo(other.lock) > 0) {
firstLock = this.lock;
secondLock = other.lock;
}
else {
firstLock = other.lock;
secondLock = this.lock;
}
synchronized(firstLock) {
synchronized(

【在 g**e 的大作中提到】
: class Num {
: private Integer i = 0;
: public synchronized void set(int i) {
: this.i = i;
: }
: public synchronized int get() {
: return i;
: }
: public synchronized void swap(Num other) {
: //implement a thread safe swap method here.

相关主题
CC150 16.6答案是不是有问题? (转载)synchronization for counters
Java synchronized method一问synchronization 锁住了什么?
发现 synchronized 的一个问题新手问个multi-threading关于synchronized和volatile的问题
进入Java版参与讨论
h*****0
发帖数: 4889
21
没初始化lock,NullPointerException

【在 F****n 的大作中提到】
: class Num {
: private final Comparable lock; // define an ordered object as lock;
: public synchronized void swap(Num other) {
: Object firstLock = null;
: Object secondLock = null;
: if (this.lock.comparesTo(other.lock) > 0) {
: firstLock = this.lock;
: secondLock = other.lock;
: }
: else {

F****n
发帖数: 3271
22
There are many ways to define an ordered sequence, which is not my interest
and left to users.

【在 h*****0 的大作中提到】
: 没初始化lock,NullPointerException
h*****0
发帖数: 4889
23
it's not trivial, it also involves sychronize issue. you should define it in
this answer.

interest

【在 F****n 的大作中提到】
: There are many ways to define an ordered sequence, which is not my interest
: and left to users.

F****n
发帖数: 3271
24
It is trivial and you can define whatever ordering in initialization. It
only needs to be a full ordering. How does it involves synchronization issue?
Ordering locks is actually a classical solution to deadlock. So no argument on it, OK?

in

【在 h*****0 的大作中提到】
: it's not trivial, it also involves sychronize issue. you should define it in
: this answer.
:
: interest

h*****0
发帖数: 4889
25
your way is good. I'm not arguing that, hehe.
but if your initializations of the locks are not synchronized, they may end
up with equal locks instead of ordered lock.

issue?
argument on it, OK?

【在 F****n 的大作中提到】
: It is trivial and you can define whatever ordering in initialization. It
: only needs to be a full ordering. How does it involves synchronization issue?
: Ordering locks is actually a classical solution to deadlock. So no argument on it, OK?
:
: in

k***r
发帖数: 4260
26
Hehe. what's not fun with these problems is that even
if you think you've got a solution, there will be no
easy way of testing it :)
T*********g
发帖数: 496
27
public synchronized void swap(Num other) {
//implement a thread safe swap method here.
Integer temp;
temp = other.i;
other.i = this.i;
this.i = temp;
}
这样会引起死锁么?
假设 a.swap(b) 那么只需要a的锁,b.swap(a)只需要b的锁,为什么会死锁呢?

【在 g**e 的大作中提到】
: class Num {
: private Integer i = 0;
: public synchronized void set(int i) {
: this.i = i;
: }
: public synchronized int get() {
: return i;
: }
: public synchronized void swap(Num other) {
: //implement a thread safe swap method here.

h*****0
发帖数: 4889
28
这样当然不会死锁,只不过可能会出错。换到一半另一个线程来换,结果一些数据就乱
了。

【在 T*********g 的大作中提到】
: public synchronized void swap(Num other) {
: //implement a thread safe swap method here.
: Integer temp;
: temp = other.i;
: other.i = this.i;
: this.i = temp;
: }
: 这样会引起死锁么?
: 假设 a.swap(b) 那么只需要a的锁,b.swap(a)只需要b的锁,为什么会死锁呢?

1 (共1页)
进入Java版参与讨论
相关主题
synchronization 锁住了什么?Java concurrency的疑惑,难道我理解错了? (转载)
新手问个multi-threading关于synchronized和volatile的问题同一个Lock锁两次,性能比较差
大家熟读了Java source code几遍?[合集] Java interview question, Thanks.
怎么synchronize时间捏interview question:
Apply lock on a class.synchronized method does lock the object that passed into the method as a parameter?
Java练习题 10请问hibernate这个功能如何实现?
Talk a little more about How to lock a filejava ,wait ,notify notifyall
请教一个多线程lock机制的问题CC150 16.6答案是不是有问题? (转载)
相关话题的讨论汇总
话题: lock话题: num话题: swap话题: public