问题描述
我使用 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.
import java.util.Calendar;
public class Task {
public void doSomething() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringBuilder sb = new StringBuilder();
//Thread Name
sb.append("Thread Name: ").append(Thread.currentThread().getName());
//Timestamp for the executing
sb.append(", elaspsed time: ").append(Calendar.getInstance().get(13)).append(" s ");
System.out.println(sb.toString());
}
}
TaskWithLock 类
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class TaskWithLock extends Task implements Runnable {
private final Lock lock = new ReentrantLock();
@Override
public void run() {
try {
lock.lock();
doSomething();
} finally {
lock.unlock();
}
}
}
TaskWithSync 类
public class TaskWithSync extends Task implements Runnable {
@Override
public void run() {
synchronized ("A") {
doSomething();
}
}
}
Main class
public class Main {
public static void runableTasks(Class<? extends Runnable> clz)
throws Exception {
ExecutorService service = Executors.newCachedThreadPool();
System.out.printf("<-- Start executing %s Task --> \n",
clz.getSimpleName());
// Start 3 threads
for (int i = 0; i < 3; i++) {
service.submit(clz.newInstance());
}
// Wait for some time, and then close the executor
TimeUnit.SECONDS.sleep(10);
System.out
.printf("<-- %s Tasks is complet --> \n", clz.getSimpleName());
service.shutdown();
}
public static void main(String[] args) throws Exception {
//Execute tasks with Lock
runableTasks(TaskWithLock.class);
//Execute tasks with Synchronized
//runableTasks(TaskWithSync.class);
}
}
第一次,通过调用方法执行Synchronized任务 runableTasks(TaskWithSync.class);
First time, executing tasks with Synchronized by calling method runableTasks(TaskWithSync.class);
<-- Start executing TaskWithSync Task -->
Thread Name: pool-1-thread-1, elaspsed time: 28 s
Thread Name: pool-1-thread-3, elaspsed time: 30 s
Thread Name: pool-1-thread-2, elaspsed time: 32 s
<-- TaskWithSync Tasks is complet -->
第二次,通过调用方法 runableTasks(TaskWithLock.class)执行带锁的任务;
Second time, executing tasks with Lock by calling method runableTasks(TaskWithLock.class);
<-- Start executing TaskWithLock Task -->
Thread Name: pool-1-thread-3, elaspsed time: 23 s
Thread Name: pool-1-thread-2, elaspsed time: 23 s
Thread Name: pool-1-thread-1, elaspsed time: 23 s
<-- TaskWithLock Tasks is complet -->
上面的例子使用 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
之间有什么区别? 在我的示例中锁定
?
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:
private final Object lock = new Object();
public void run() {
synchronized (lock) {
doSomething();
}
}
这更好,因为隐藏了外部对象的锁,您正在封装同步协议,从而实现更安全的同步策略。
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
,前者是同步原语,而后者是更高级别的锁定构造,提供比同步。
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
.
您可以查看更多信息和。
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.
这篇关于在我的例子中,Synchronized和Lock有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!