由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - synchronization 锁住了什么?
相关主题
synchronization for countersApply lock on a class.
发现 synchronized 的一个问题问一个基础问题
一个关于generics的问题请教:performance issue
新手问个multi-threading关于synchronized和volatile的问题请教:怎么把synchronize的method改成用thread 但thread safe呢?
Talk a little more about How to lock a filejava的volatile
请教一个多线程lock机制的问题Java concurrency的疑惑,难道我理解错了? (转载)
怎么synchronize时间捏synchronized method does lock the object that passed into the method as a parameter?
HashMap cache传递一个object reference,如何防止更改object?
相关话题的讨论汇总
话题: object话题: int话题: intarray话题: lock
进入Java版参与讨论
1 (共1页)
I***e
发帖数: 1136
1
假设我有一个class:
public class A{
String s;
Integer i;
int i1;
HashMap hw;
B b;
}
public class B{
...
}
如果用
A a;
synchronized(a){
...
}
那么我所住了什么? hw的锁是否被我取得? 那hw的元素的呢? 比如说hw是从String到
String的map, 我可以改变其中一个value吗?
另外, 是否b也被锁住了?
谢谢.
c*****t
发帖数: 1879
2
synchronize (a) put lock inside the a object, which would block of
any other actions that synchronize on the same object, or calling
synchronized methods in A.
However, methods that doesn't use "synchronize (a)" can still access
a's values w/o blocking.

【在 I***e 的大作中提到】
: 假设我有一个class:
: public class A{
: String s;
: Integer i;
: int i1;
: HashMap hw;
: B b;
: }
: public class B{
: ...

g*****g
发帖数: 34805
3
Yes, basically a flag is checked, then set before entering synchronize(a), and
clear after exit. if others don't check this flag (use synchronize too),
they do whatever they want.

【在 c*****t 的大作中提到】
: synchronize (a) put lock inside the a object, which would block of
: any other actions that synchronize on the same object, or calling
: synchronized methods in A.
: However, methods that doesn't use "synchronize (a)" can still access
: a's values w/o blocking.

I***e
发帖数: 1136
4
I understand that part. Howver, if I say synchronized(a){...}
and then elsewhere synchronized(b){...} where b is a reference to a
B type object, which happened to be the object that a.b is reference of,
do these two locks affect each other?
Basically, if you acquire a lock on a, do you have a lock on all of 'a''s
member objects?
Thanks,
-iCare-

and

【在 g*****g 的大作中提到】
: Yes, basically a flag is checked, then set before entering synchronize(a), and
: clear after exit. if others don't check this flag (use synchronize too),
: they do whatever they want.

g*****g
发帖数: 34805
5
Lock locks the block or the method you want to sync, not the object itself.
Unsync methods can still access the object.

【在 I***e 的大作中提到】
: I understand that part. Howver, if I say synchronized(a){...}
: and then elsewhere synchronized(b){...} where b is a reference to a
: B type object, which happened to be the object that a.b is reference of,
: do these two locks affect each other?
: Basically, if you acquire a lock on a, do you have a lock on all of 'a''s
: member objects?
: Thanks,
: -iCare-
:
: and

I***e
发帖数: 1136
6
So let's say I have 2 blocks of code, and 2 separate threads hitting them at
the same time:
synchronized(a){
// do something here
a.b.change();
}
synchronized(b){
// do something else
change b.change();
}
what will happen? Do I get to handle them one by one or
do these locks prevent the the blocks to be executed
simultaneously?
Thx.

【在 g*****g 的大作中提到】
: Lock locks the block or the method you want to sync, not the object itself.
: Unsync methods can still access the object.

g*****g
发帖数: 34805
7
The result is unpredictable, you should sync change() instead.
If you want to lock data, lock the codes
that access these data instead.

【在 I***e 的大作中提到】
: So let's say I have 2 blocks of code, and 2 separate threads hitting them at
: the same time:
: synchronized(a){
: // do something here
: a.b.change();
: }
: synchronized(b){
: // do something else
: change b.change();
: }

I***e
发帖数: 1136
8
In my scenario, b is already defined and I can only use it as is.

【在 g*****g 的大作中提到】
: The result is unpredictable, you should sync change() instead.
: If you want to lock data, lock the codes
: that access these data instead.

g*****g
发帖数: 34805
9
Then you have to figure it out by yourself. Ugly design can have ugly fix,
but that's not the point of this board.

【在 I***e 的大作中提到】
: In my scenario, b is already defined and I can only use it as is.
m******t
发帖数: 2416
10

The answer is no.

【在 I***e 的大作中提到】
: In my scenario, b is already defined and I can only use it as is.
相关主题
请教一个多线程lock机制的问题Apply lock on a class.
怎么synchronize时间捏问一个基础问题
HashMap cache请教:performance issue
进入Java版参与讨论
I***e
发帖数: 1136
11

Thanks.
Thanks to goodbug too.
no bug is like goodbug.
-iCare-

【在 m******t 的大作中提到】
:
: The answer is no.

p***p
发帖数: 559
12
http://www.javaworld.com/javaworld/jw-07-1997/jw-07-hood-p2.html

【在 I***e 的大作中提到】
:
: Thanks.
: Thanks to goodbug too.
: no bug is like goodbug.
: -iCare-

p***p
发帖数: 559
13
synchornized(obj)
{
Codes to be protected
}
1 what i want to protected is the code, not the obj. To be sure only 1 thread
excute the codes a time. It is ok.
But, Question
2 so how should I choice the Obj, for design? When should I use synchornized(this)? what does it mean, for design.
synchornized(this) {
......
}
3 for
Class myclass {
int myObj=1;
synchornized void myMethod() { // not static
int something = myObj;
..........
}
}
myclass mca,mcb;
mca.myMethod();
mcb.myMethod()

【在 g*****g 的大作中提到】
: Then you have to figure it out by yourself. Ugly design can have ugly fix,
: but that's not the point of this board.

c*****t
发帖数: 1879
14

Well, this is placing lock on obj. There is no such thing as code to be
protected, what this saying is really
obj logically cover the access right of the resources which should
not be concurrently accessed. and the following code need to access
these resources.
thread
(this)? what does it mean, for design.
Depend on if you want to have finer control of resources. If you want to
update a HashMap instance variable, for example, you would only care if
reading/writing to

【在 p***p 的大作中提到】
: synchornized(obj)
: {
: Codes to be protected
: }
: 1 what i want to protected is the code, not the obj. To be sure only 1 thread
: excute the codes a time. It is ok.
: But, Question
: 2 so how should I choice the Obj, for design? When should I use synchornized(this)? what does it mean, for design.
: synchornized(this) {
: ......

p***p
发帖数: 559
15
class KitchenSync {
private int[] intArray = new int[10];
void reverseOrder() {
synchronized (this) {
int halfWay = intArray.length / 2;
for (int i = 0; i < halfWay; ++i) {
int upperIndex = intArray.length - 1 - i;
int save = intArray[upperIndex];
intArray[upperIndex] = intArray[i];
intArray[i] = save;
}
}
}
}
In the case above, the statements contained within the synch

【在 c*****t 的大作中提到】
:
: Well, this is placing lock on obj. There is no such thing as code to be
: protected, what this saying is really
: obj logically cover the access right of the resources which should
: not be concurrently accessed. and the following code need to access
: these resources.
: thread
: (this)? what does it mean, for design.
: Depend on if you want to have finer control of resources. If you want to
: update a HashMap instance variable, for example, you would only care if

c*****t
发帖数: 1879
16

before
All it is saying is that you have to have an object to place the lock in,
could be "this" object, or any other objects. The code is blocked until
the lock is aquired.
The following code is identical in terms of functionality of the code you
had. I just choose the object containing the lock to be the array itself.
class Foo
{
private int[] intArray = new int[10];
public void reverseOrderA ()
{
// must make assumption that intArray is never null
synchronized (intArra

【在 p***p 的大作中提到】
: class KitchenSync {
: private int[] intArray = new int[10];
: void reverseOrder() {
: synchronized (this) {
: int halfWay = intArray.length / 2;
: for (int i = 0; i < halfWay; ++i) {
: int upperIndex = intArray.length - 1 - i;
: int save = intArray[upperIndex];
: intArray[upperIndex] = intArray[i];
: intArray[i] = save;

p***p
发帖数: 559
17
SO
Class myfriend {
private Human[] girls = new Human[10];
private Human[] boys = new Human[20];
// all Method on Girl must be Synchornized
protected dateGirl(int i) {
Synchornized(girls) {
this.date(girls[i]);
}
}
protected dumpGirl(int i) {
Synchornized(girls) {
this.dump(girls[i]);
this.info(boys,girls[i]); // ???
}
}

// if i am a lazy programmer, just Lock "this"
protected Synchornized kissGirl(int i) {
this.kiss(girls[i])
}
}
My Quest

【在 c*****t 的大作中提到】
:
: before
: All it is saying is that you have to have an object to place the lock in,
: could be "this" object, or any other objects. The code is blocked until
: the lock is aquired.
: The following code is identical in terms of functionality of the code you
: had. I just choose the object containing the lock to be the array itself.
: class Foo
: {
: private int[] intArray = new int[10];

c*****t
发帖数: 1879
18
Okay, I see a major confusion you have.
1. As I said before, the purpose of lock is to protect Resources, not code.
2. Only code that has synchornization block (or in the declaration) can
be potentially blocked from reading/writing access of resources.
So you could have
class Foo
{
private Vector m_list = new Vector ();
public synchronized add (Object o) { m_list.add (o); }
public Object get (int index) { m_list.get (i); }
}
This piece of code has a problem.

【在 p***p 的大作中提到】
: SO
: Class myfriend {
: private Human[] girls = new Human[10];
: private Human[] boys = new Human[20];
: // all Method on Girl must be Synchornized
: protected dateGirl(int i) {
: Synchornized(girls) {
: this.date(girls[i]);
: }
: }

p***p
发帖数: 559
19
Opps
for
class Foo
{
private Vector m_list = new Vector ();
public synchronized add (Object o) { m_list.add (o); }
public Object get (int index) { m_list.get (i); }
}
is it ok?
class Foo
{
private Vector m_list = new Vector ();
public add (Object o) {
synchronized(m_list) {
m_list.add (o); }
}
public Object get (int index) {
synchronized(m_list) {
m_list.get (o); }
}
}
}
or some lazy
clas

【在 c*****t 的大作中提到】
: Okay, I see a major confusion you have.
: 1. As I said before, the purpose of lock is to protect Resources, not code.
: 2. Only code that has synchornization block (or in the declaration) can
: be potentially blocked from reading/writing access of resources.
: So you could have
: class Foo
: {
: private Vector m_list = new Vector ();
: public synchronized add (Object o) { m_list.add (o); }
: public Object get (int index) { m_list.get (i); }

m******t
发帖数: 2416
20

Hmm, this is correct but isn't entirely accurate - obj does not necessarily
cover the resource that need to be controlled, if there is some resource
at all. Technically you can synchronize on obj and do things inside
the synch block that completely don't have to do with obj. In fact,
it's a useful technique to use an Object instance as a primitive
semaphore, and create a critical section by synchronizing on it.

【在 c*****t 的大作中提到】
: Okay, I see a major confusion you have.
: 1. As I said before, the purpose of lock is to protect Resources, not code.
: 2. Only code that has synchornization block (or in the declaration) can
: be potentially blocked from reading/writing access of resources.
: So you could have
: class Foo
: {
: private Vector m_list = new Vector ();
: public synchronized add (Object o) { m_list.add (o); }
: public Object get (int index) { m_list.get (i); }

1 (共1页)
进入Java版参与讨论
相关主题
传递一个object reference,如何防止更改object?Talk a little more about How to lock a file
问个面试题, 谢谢请教一个多线程lock机制的问题
CC150 16.6答案是不是有问题? (转载)怎么synchronize时间捏
Re: Question: Java SynchronizationHashMap cache
synchronization for countersApply lock on a class.
发现 synchronized 的一个问题问一个基础问题
一个关于generics的问题请教:performance issue
新手问个multi-threading关于synchronized和volatile的问题请教:怎么把synchronize的method改成用thread 但thread safe呢?
相关话题的讨论汇总
话题: object话题: int话题: intarray话题: lock