What#39;s the difference between Synchronized and Lock in my example?(在我的示例中,同步和锁定有什么区别?)
问题描述
我写了一个简单的代码来使用 Lock
和 synchronized
来模拟并发.
I wrote a simple code to mock concurrency using Lock
and synchronized
.
源码如下:
任务类包含一个名为 doSomething()
的方法,用于打印线程名称和执行经过的时间.
Task class includes a method named doSomething()
to print the thread name and executing elapsed time.
TaskWithLock 类
TaskWithSync 类
主类
第一次,通过调用runableTasks(TaskWithSync.class)方法同步执行任务;
第二次,调用方法runableTasks(TaskWithLock.class);
上面的例子使用 Lock
和 synchronized
来运行任务,我这里有一些问题:
With the above example using Lock
and synchronized
to run the tasks, I have some questions here:
Q1: 同步示例中的时间戳,它反映了 3 个线程之间的互斥量.但是为什么 Lock 例子中的经过时间是一样的呢?我不知道为什么.
Q1: The timestamp in the synchronized example, it reflects the mutex between 3 Threads. But why is the elapsed time in Lock example the same? I don't know why.
Q2:在我的例子中synchronized
和Lock
有什么区别?
Q2: What's the difference between synchronized
and Lock
in my example?
请帮我解决这两个问题.
Please help me out with these 2 questions.
推荐答案
首先,您使用 synchronized
的示例是错误的:在对象A"上同步是一个非常糟糕的主意.请改用以下成语:
First of all, your example using synchronized
is ill conceived: it is a very bad idea to synchronize on object "A". Use the following idiom instead:
这更好,因为通过对外部对象隐藏您的锁,您可以封装您的同步协议,从而实现更安全的同步策略.
This is better because by hiding your lock from external objects, you are encapsulating your synchronization protocol and thereby implementing a safer synchronization policy.
现在,synchronized
和 java.util.concurrent.locks.Lock
之间的区别在于,前者是同步原语,而后者是更高级别的锁定结构它提供了比 synchornized
更精细的操作.
Now, the difference between synchronized
and java.util.concurrent.locks.Lock
, is that the former is a synchronization primitive whereas the latter a higher level locking construct which provides more elaborate operations than synchornized
.
更多信息,您可以查看 http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Lock.html 和 http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/特别是 locks/ReentrantLock.html.
Fore more information you may look at http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Lock.html and http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/ReentrantLock.html in particular.
这篇关于在我的示例中,同步和锁定有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!