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.
|
|
|
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.
|
|
|
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的锁,为什么会死锁呢?
|