This question already has an answer here: Avoid synchronized(this) in Java? 18 answers Either way is fine. There are design reasons to use your object as the lock, and there are design reasons to keep the lock private as you have done. The only critisizm I would make is that Object typically gets used as a lock in Java, you don't have to invent a dummy class. public class MsLunch {
这个问题在这里已经有了答案: 避免在Java中同步(this)? 18个答案 无论哪种方式都很好。 有设计理由使用你的对象作为锁,并且有设计理由让你的锁保持私密。 我要做的唯一批评就是Object通常被用作Java中的锁,你不必发明一个虚拟类。 public class MsLunch { private long c1 = 0; private long c2 = 0; private final Object lock1 = new Object(); private final Object lock2 = new Object();
This question already has an answer here: Avoid synchronized(this) in Java? 18 answers First some basics: The synchronization object is used as a key to open a door to a restricted area (the synchronization block). As long as a thread entered this restricted area it holds the monitor (lock) so that no other thread can enter. When a thread exits the restricted area it releases the monito
这个问题在这里已经有了答案: 避免在Java中同步(this)? 18个答案 首先是一些基础知识: 同步对象用作打开限制区域的门(同步块)的键。 只要一个线程进入这个限制区域,它就拥有监视器(锁定),以便其他线程不能进入。 当一个线程退出受限区域时,它释放监视器,另一个线程可以接受它。 这意味着每个使用相同同步对象的同步块将阻止其他线程进入,直到显示器可用(解锁)。 创建新的虚拟对象的原因是什么?
This question already has an answer here: Avoid synchronized(this) in Java? 18 answers What this means is that more than one threads cannot enter these two synchronized blocks (or one of them) at 'the same time' . Whether it works depends on what exactly you want to achieve. The thread which is currently in such a synchronized block is said to own the object's monitor at that
这个问题在这里已经有了答案: 避免在Java中同步(this)? 18个答案 这意味着,不止一个线程不能 “同时”进入这两个同步块(或其中一人)。 它是否有效取决于你想要达到的目标。 这是目前在这样的同步块中的线程被认为拥有该对象的显示器在那个时间瞬间(在这种情况下,对象的监视器由指向this )。 线程在this上同步。 所以同一时间只有一个线程在类中(假设没有其他方法)。 您没有单独同步这些方法。 所以这可能
Possible Duplicate: Avoid synchronized(this) in Java? What is the difference between the two pieces of code ? What are advantages and disadvantages of each? 1) public class Example { private int value = 0; public int getNextValue() { synchronized (this) { return value++; } } } 2) public class Example { private int value = 0; private final Object lock = new Object(); p
可能重复: 避免在Java中同步(this)? 这两段代码有什么区别? 每个的优缺点是什么? 1) public class Example { private int value = 0; public int getNextValue() { synchronized (this) { return value++; } } } 2) public class Example { private int value = 0; private final Object lock = new Object(); public int getNextValue() { synchronized (lock) { return value
When we are using wait and notify in thread environment. I have a class to process data as background process. And when there is no data to process it should call wait. synchronized(some_object){ wait(); } In another class I am adding the data again. I need call notify() method. synchronized(some_object){ runnabale_object.notify(); } Why i should use same object for synchronized bloc
当我们在线程环境中使用wait和notify时。 我有一个类来处理数据作为后台进程。 当没有数据需要处理时,应该等待。 synchronized(some_object){ wait(); } 在另一课中,我再次添加数据。 我需要调用notify()方法。 synchronized(some_object){ runnabale_object.notify(); } 为什么我应该在这两个不同的类中为同步块使用相同的对象。 正如我读同步习惯 “同步”关键字阻止多个线程同时访问一段代码或对象。 但是
While I know the theoretical differences between Re-EntrantLocks and synchronized , I'm confused to the below point. See this statement from an article on Javarevisited comparing synchronized and Lock objects: One more worth noting difference between ReentrantLock and synchronized keyword in Java is, ability to interrupt Thread while waiting for Lock. In case of synchronized keyword, a t
虽然我知道Re-EntrantLocks和synchronized之间的理论差异,但我对以下几点感到困惑。 从JavaRevisited比较synchronized和Lock对象的文章中看到这个陈述: 在Java中,ReentrantLock和synchronized关键字之间还有一点值得注意的是,能够在等待锁定时中断Thread。 在同步关键字的情况下,一个线程可以被阻塞等待锁定,无限期的时间,并且没有办法控制它。 ReentrantLock提供了一个名为lockInterruptibly()的方法,它可以在
I have a code piece that I am reviewing (using FindBugs). public class MyClass{ ... private BlockedQueue q = new LinkedBlockingQueue<MyData>(1000); private static final batchSize = 1000; public boolean testMethod(){ boolean done = false; synchronized(q){ if(q.size == batchSize){ q.notify(); done = true; } } return done; } When I run FindB
我有一个代码片,我正在审查(使用FindBugs)。 public class MyClass{ ... private BlockedQueue q = new LinkedBlockingQueue<MyData>(1000); private static final batchSize = 1000; public boolean testMethod(){ boolean done = false; synchronized(q){ if(q.size == batchSize){ q.notify(); done = true; } } return done; } 当我在这段代码上运行FindBugs
I have read Java Concurrency in Practice and this is a great reference, but I would like to see a concise single page summary of the use cases of the java.util.concurrent package. For instance: Why use a concurrent collection over a synchronized collection? When should the atomic classes be preferred over explicit locking? When should Locks be used over synchronization? What are the alt
我已经阅读过Java Concurrency in Practice,这是一个很好的参考,但我希望看到java.util.concurrent包用例的简单单页摘要。 例如: 为什么在同步集合上使用并发集合? 原子类何时应该优于显式锁定? Locks何时应该用于同步? wait()和notify(),notifyAll()是什么选择? 什么时候应该使用CompletionService? 有什么优点/缺点和陷阱需要注意? 为什么在同步集合上使用并发集合? 因为synchronized收集
How do atomic / volatile / synchronized work internally? What is the difference between the following code blocks? Code 1 private int counter; public int getNextUniqueIndex() { return counter++; } Code 2 private AtomicInteger counter; public int getNextUniqueIndex() { return counter.getAndIncrement(); } Code 3 private volatile int counter; public int getNextUniqueIndex() {
原子/易失性/同步如何在内部工作? 以下代码块有什么区别? 代码1 private int counter; public int getNextUniqueIndex() { return counter++; } 代码2 private AtomicInteger counter; public int getNextUniqueIndex() { return counter.getAndIncrement(); } 代码3 private volatile int counter; public int getNextUniqueIndex() { return counter++; } 以下列方式进行volatile工作吗? 是 vo
While discussing a Java synchronization question, someone made a comment that the following snippets are not equivalent (and may compile to different bytecodes): public synchronized void someMethod() { //stuff } and public void someMethod() { synchronized (this) { //stuff } } Are they equivalent? They are equivalent in function, though the compilers I tested (Java 1.6.0_07 and Ecl
在讨论Java同步问题时,有人发表评论说以下片段不等同(可能编译为不同的字节码): public synchronized void someMethod() { //stuff } 和 public void someMethod() { synchronized (this) { //stuff } } 它们是否相同? 它们在功能上是等价的,尽管我测试的编译器(Java 1.6.0_07和Eclipse 3.4)生成不同的字节码。 第一个生成: // access flags 33 public synchronized someMethod()V RETURN 第二个