每次在非最终类字段上同步时都会显示警告.这是代码:
public class X
{
private Object o;
public void setO(Object o)
{
this.o = o;
}
public void x()
{
synchronized (o) // synchronization on a non-final field
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以我用以下方式改变了编码
public class X
{
private final Object o;
public X()
{
o = new Object();
}
public void x()
{
synchronized (o)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定上面的代码是在非final类字段上同步的正确方法.如何同步非最终字段?
但是,除了相互排斥之外,还有更多的同步.同步确保线程在同步块之前或期间的内存写入以可预测的方式显示给在同一监视器上同步的其他线程.在我们退出synchronized块之后,我们释放了监视器,它具有将缓存刷新到主内存的效果,因此该线程所做的写操作对其他线程是可见的.在我们进入同步块之前,我们获取监视器,它具有使本地处理器高速缓存无效的效果,以便从主存储器重新加载变量.然后,我们将能够看到前一版本中显示的所有写入.
我还记得在现代Sun VM上阅读,无竞争同步很便宜.这个说法我有点困惑.考虑以下代码:
class Foo {
int x = 1;
int y = 1;
..
synchronized (aLock) {
x = x + 1;
}
}
Run Code Online (Sandbox Code Playgroud)
对x的更新需要同步,但是锁的获取是否也从缓存中清除了y的值?我无法想象会出现这种情况,因为如果确实如此,锁定条带化等技术可能无济于事.或者,JVM可以可靠地分析代码以确保使用相同的锁在另一个同步块中不修改y,因此在进入同步块时不会在缓存中转储y的值吗?
我声明我读过线程,但我从未使用过.所以我问你:)
我有两个线程:A和B,其中A管理GUI和B管理逻辑.
我会先说A.
然后在A绘制GUI时,我会暂停它,等待B到达X点进入run方法.
当B达到X点进入运行方法时,我暂停B,然后恢复A.
A并B共享一些变量来管理GUI,逻辑......
我可以做吗?如果有,怎么样?:)
什么是Java相当于ManualResetEvent?
我了解到调用Object的wait()方法将释放对象监视器(如果存在).
但我有一些关于notify()通过另一个线程调用此对象的问题:
如果另一个(第3个)线程同时拥有对象监视器,那么等待线程是否会被唤醒?
如果第三个线程调用wait()此对象,那么等待线程会被唤醒吗?
是否可以确定线程是否在等待通知特定对象(java 1.4/java 5)
如果wait()在finalize()方法中调用会发生什么?
仅限Java 5及以上版本.假设一个多处理器共享内存计算机(你现在可能正在使用它).
这是一个单例的延迟初始化代码:
public final class MySingleton {
private static MySingleton instance = null;
private MySingleton() { }
public static MySingleton getInstance() {
if (instance == null) {
synchronized (MySingleton.class) {
if (instance == null) {
instance = new MySingleton();
}
}
}
return instance;
}
}
Run Code Online (Sandbox Code Playgroud)
是否instance必须声明volatile,以阻止优化重写的getInstance()如下(这将是一个连续的程序是正确的):
public static MySingleton getInstance() {
if (instance == null) {
synchronized (MySingleton.class) {
// instance must be null or we wouldn't be here (WRONG!)
instance = new MySingleton(); …Run Code Online (Sandbox Code Playgroud) 我有生产者和消费者联系BlockingQueue.
消费者从队列中等待记录并处理它:
Record r = mQueue.take();
process(r);
Run Code Online (Sandbox Code Playgroud)
我需要从其他线程暂停这个过程一段时间.怎么实现呢?
现在我认为实现它,但它似乎是不好的解决方案:
private Object mLock = new Object();
private boolean mLocked = false;
public void lock() {
mLocked = true;
}
public void unlock() {
mLocked = false;
mLock.notify();
}
public void run() {
....
Record r = mQueue.take();
if (mLocked) {
mLock.wait();
}
process(r);
}
Run Code Online (Sandbox Code Playgroud) 我正在学习并发任务之间的合作,我已经得到了这个问题和可能的答案。我想确保我理解正确。
\n\n因此,要调用a.wait()它,首先需要在对象上同步a,或者更准确地说,线程必须成为a监视器的所有者。据我了解,a.wait()应该在同步上下文中调用(与a.notify()/一起a.notifyAll())的唯一原因是为了避免竞争条件/丢失唤醒问题。但理论上,可以通过在其他对象上同步来避免竞争条件调用,如下所示a.wait():a.notify()/a.notifyAll()
Thread #1: \nsynchronized(b) { \n \xe2\x80\xa6\n a.wait(); // here a thread owns only one monitor: the monitor of the object stored in the variable b, where a != b\n \xe2\x80\xa6\n}\n\nThread #2: \nsynchronized(b) {\n \xe2\x80\xa6\n a.notify(); // the same here\n \xe2\x80\xa6\n}\nRun Code Online (Sandbox Code Playgroud)\n\nThe expected behavior is: the thread #1 acquires the monitor of b, enters the critical section, invokes …
尝试notifyAll()在synchronized语句中执行调用时出现以下错误:在同步上下文外调用Object.notify().
例:
final List list = new ArrayList();
synchronized(list) {..... invoked notifyAll() here};
Run Code Online (Sandbox Code Playgroud) 我没有看到以下代码如何产生看似违反对象锁定义的输出.当然只允许一个线程打印"获取锁定"消息,但他们都这样做?
class InterruptThreadGroup {
public static void main(String[] args) {
Object lock = new Object();
MyThread mt1 = new MyThread(lock);
MyThread mt2 = new MyThread(lock);
mt1.setName("A");
mt1.start();
mt2.setName("B");
mt2.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
// Thread.currentThread().getThreadGroup().interrupt();
}
}
class MyThread extends Thread {
private Object lock;
public MyThread(Object l) {
this.lock = l;
}
public void run() {
synchronized (lock) {
System.out.println(getName() + " acquired lock");
try {
lock.wait();
} catch (InterruptedException e) {
System.out.println(getName() + …Run Code Online (Sandbox Code Playgroud) java ×10
concurrency ×2
locking ×2
synchronized ×2
wait ×2
.net ×1
finalizer ×1
jvm ×1
notify ×1
volatile ×1