同步 Getter 和 Setter
我现在正在研究Java并发。我有一个关于同步
和锁的问题。
对于任何可变数据,我们应该将所有访问数据的方法放在同一个锁中。
但是,同一个锁是什么意思呢?
示例:
public class SynchronizedInteger{
private int value;
public synchronized int get(){return value;}
public synchronized void set(int value){this.value=value;}
}
那么,我的问题是为什么这两个方法处于同一个锁中?我知道是这样,但我想知道为什么?而且,这是否意味着类中的所有同步方法都处于同一个锁中?
编辑:
所以,如果我向类中添加一种方法:
公共同步无效 printOneToHunder(){
for(int i=0;i<100;i++) System.out.println(i);
}
这个方法也会被包含在与 setter 和 getter 相同的块中吗?那么,当有一个线程使用 setter 或 getter 时,其他线程无法运行此方法吗?
而且,如果我将类更改为以下内容会怎么样:
public class SynchronizedInteger{
private int value1;
private int value2;
public synchronized int get1(){return value1;}
public synchronized void set1(int value){this.value1=value1;}
public synchronized int get2(){return value2;}
public synchronized void set2(int value){this.value2=value2;}
}
据我现在的理解,一次只有一个线程可以调用这些方法。那么有什么方法可以让一个线程可以修改value1而另一个线程可以修改value2呢???
非常感谢您的热心解惑!!!!
I am studying Java concurrency right now. I have a question about synchronized
and locks.
For any mutable data, we should put all the method accessing the data in the same lock.
But, what does same lock mean?
Example:
public class SynchronizedInteger{
private int value;
public synchronized int get(){return value;}
public synchronized void set(int value){this.value=value;}
}
So, my question is why this two methods are in the same lock? I know they are, but I would like to know why? And, does it mean that all the synchronized methods in the class are in the same lock?
Edit:
so, if I added one more method to the class:
public synchronized void printOneToHunder(){
for(int i=0;i<100;i++) System.out.println(i);
}
This method will also be included into same block as setter and getter? So, other thread cannot run this method when there is one thread using either setter or getter?
And, what if I change the class to below:
public class SynchronizedInteger{
private int value1;
private int value2;
public synchronized int get1(){return value1;}
public synchronized void set1(int value){this.value1=value1;}
public synchronized int get2(){return value2;}
public synchronized void set2(int value){this.value2=value2;}
}
For my understanding right now, only one thread can call these method at one time. So what's the way to make one thread can modify value1 and the other thread to modify value2???
Thanks a lot for your kindly clarifications!!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您声明
synchronized
的所有非静态方法的行为本质上就好像代码是这样的:即,对
this
采取了隐式锁定。因此,所有非静态同步方法都会锁定调用它们的实例。静态同步方法锁定类对象本身,而不是该类的实例。
All non-static methods that you declare
synchronized
behave essentially as if the code was:i.e. there is an implicit lock on
this
taken. So all non-static synchronized methods will lock the instance on which they are called.Static synchronized methods take a lock on the class object itself, rather than an instance of that class.
是的,您的两种方法使用相同的锁。
不带参数的
synchronized
关键字隐式转换为syncrhonized(this)
,这对于两种方法来说是同一个对象。为了让两个块在java中具有“相同的锁”,这意味着它们都使用相同的对象作为锁。正如我所说,在您的情况下,两种方法都使用
this
作为锁定对象。Yes, your two methods are using the same lock.
The
synchronized
keyword without parameter is implicitly translated tosyncrhonized(this)
, which is the same object for both methods.In order for two blocks to have "the same lock" in java, it means that they are both using the same object as a lock. And as I said, in your case, both methods are using
this
as the lock object.当您声明一个方法
synchronized
时,这意味着同步是在调用该方法的特定实例上进行的。When you declare a method
synchronized
, it means that synchronization is on the particular instance for which the method is being invoked.但您还有其他同步方法。
同步块:
synchronized(someLockedObj){somecode}
锁定对象:
锁lock = new ReentrantLock();锁.lock(); lock.unlock();
But you have other ways for synchronization.
Synchronized blocks:
synchronized(someLockedObj){somecode}
Lock-Objects:
Lock lock = new ReentrantLock(); lock.lock(); lock.unlock();