每当有关Java同步的问题出现时,有些人非常渴望指出synchronized(this)应该避免的.相反,他们声称,首选锁定私人参考.
一些给出的原因是:
其他人,包括我在内,认为这synchronized(this)是一个被大量使用的习惯用法(也在Java库中),是安全且易于理解的.它不应该被避免,因为你有一个错误,你不知道多线程程序中发生了什么.换句话说:如果适用,则使用它.
我有兴趣看到一些现实世界的例子(没有foobar的东西)避免锁定this是最好的,当synchronized(this)也做的工作.
因此:您是否应始终避免synchronized(this)并使用私有引用上的锁来替换它?
一些进一步的信息(更新为答案):
synchronized方法)和显式形式synchronized(this)synchronized(this)提供,那么synchronized(this)不适用,所以这不是问题我试图理解是什么让并发锁如此重要,如果可以使用synchronized (this).在下面的虚拟代码中,我可以做到:
码:
private final ReentrantLock lock = new ReentrantLock();
private static List<Integer> ints;
public Integer getResult(String name) {
.
.
.
lock.lock();
try {
if (ints.size()==3) {
ints=null;
return -9;
}
for (int x=0; x<ints.size(); x++) {
System.out.println("["+name+"] "+x+"/"+ints.size()+". values >>>>"+ints.get(x));
}
} finally {
lock.unlock();
}
return random;
}
Run Code Online (Sandbox Code Playgroud) java.util.concurrentAPI提供了一个名为as的类Lock,它基本上将序列化控件以访问关键资源.它给出了诸如park()和的方法unpark().
我们可以做类似的事情,如果我们可以使用synchronized关键字,并使用wait()和notify() notifyAll()方法.
我想知道其中哪一个在实践中更好,为什么?
java concurrency multithreading synchronization java.util.concurrent
我尝试在onPostExecute中通知适配器的主类列表视图,但是我收到错误:java.lang.IllegalMonitorStateException:对象在notify()之前没有被线程锁定
@Override
protected void onPostExecute(String result) {
popularfragment.adapter.notifyDataSetChanged();
recentfragment.adapter.notifyDataSetChanged();
}
Run Code Online (Sandbox Code Playgroud) 内联Java IDE提示声明,"在循环中调用Thread.sleep会导致性能问题." 我在文档的其他地方找不到解释.这个说法.
为什么?怎么样?还有什么其他方法可以延迟线程的执行?
我不确定采用哪种策略......我专注于我的操作完成,但我也想将性能问题保持在最低限度......有一种名为Execute()的方法必须等待(同步运行)直到操作完成.此操作发生在另一个线程上.有两种方法可以实现同样的事情......
通过使用ManualResetEvent
void Execute()
{
taskHandle = new ManualResetEvent(false);
.
.
//delegate task to another thread
.
.
taskHandle.WaitOne();
}
Run Code Online (Sandbox Code Playgroud)
通过使用简单的while构造
void Execute()
{
.
.
//delegate task to another thread
.
.
while (!JobCompleted)
Thread.Sleep(1000);
}
Run Code Online (Sandbox Code Playgroud)
我应采用两种方法中的哪一种......为什么?
编辑:
Q2.如果我在构造时只是空了怎么办?有什么不同...?
while(!JobCompleted);
Run Code Online (Sandbox Code Playgroud)
编辑:(之前我收集过的东西)
http://www.yoda.arachsys.com/csharp/threads/waithandles.shtml - 这篇文章说手动复制比较慢,因为它们离开了托管代码并重新进入......
我有个问题.当我notify()在synchronized块中使用时,我有IllegalMonitorStateException.任何人都可以帮我解决这个问题吗?
我必须这样做,一个线程将发送到第二个线程char,然后这个线程必须等待,第二个线程打印此char.在第二个线程等待之后,第一个再次发送下一个char
Main.java:
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
public class Main extends JFrame {
Thread t1, t2;
Consumer con;
public Main() {
con = new Consumer();
startThreads();
}
private synchronized void startThreads() {
t1 = new Thread(new Producent("grudzien", con));
t1.start();
t2 = new Thread(con);
t2.start();
}
public class Producent implements Runnable {
String m_atom;
char[] atoms; …Run Code Online (Sandbox Code Playgroud) 我得到了java.lang.IllegalMonitorStateException.我提到了这个问题,它解决了我的问题.第一个答案是
To be able to call notify() you need to synchronize on the same object.
synchronized (someObject) {
someObject.wait();
}
/* different thread / object */
synchronized (someObject) {
someObject.notify();
}
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么我们需要在同一个对象广告上同步它的工作原理?
据我所知,据我所知
synchronized (someObject) {
someObject.wait();
}
Run Code Online (Sandbox Code Playgroud)
我们得到对象someObject的锁,然后我们调用wait().现在,另一个线程怎么能锁定同一个对象来调用notify()呢?我错过了什么?
我曾经写过一个synchronized像以下一样的块:
synchronized(foobar) {
// do something
}
Run Code Online (Sandbox Code Playgroud)
但是,最近我看到有人写道:
synchronized(foobar) {
// do something
foobar.notifyAll();
}
Run Code Online (Sandbox Code Playgroud)
有foobar.notifyAll();必要吗?如果我省略它会怎么样?
我正在进行一项任务,必须创建两个类,一个代表一个人,另一个代表一个桥.任何时候只有一个人可以"穿越"这座桥,但可能有人在等待穿越
我通过多线程轻松实现了这一功能,允许多个人同时进行交叉,但是在更改它时只有一个线程运行时我遇到了问题...
我的主要问题是他们想要的类设计,我必须在person类中开始线程,但是桥类需要能够等待并通知它们启动/停止
我有什么想法可以做到这一点?
执行while循环时,java会不断检查参数是否为真?例如,如果是while循环
while(k=7) {
Thread.sleep(5000);
}
Run Code Online (Sandbox Code Playgroud)
但是在5秒内另一个线程将k改为8,循环会继续等待还是立即退出?
HI编写了一个示例程序,用于测试java中的wait行为.
我的Runnable实现:
class ThreadWait implements Runnable {
Object lock = new Object();
ThreadWait(Object lock){
this.lock = lock;
}
@Override
public void run() {
try {
synchronized (lock){
System.out.println("Started : "+Thread.currentThread().getName());
wait();
System.out.println("Completed : "+Thread.currentThread().getName());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的main喜欢中使用:
Object lock = new Object();
ThreadWait t1 = new ThreadWait(lock);
ThreadWait t2 = new ThreadWait(lock);
Thread a= new Thread(t1);
a.setName("A");
Thread b= new Thread(t2);
b.setName("B");
a.start();
b.start();
Run Code Online (Sandbox Code Playgroud)
运行此程序时,我收到此异常:
Exception in thread "A" Exception …Run Code Online (Sandbox Code Playgroud) java ×11
concurrency ×2
notify ×2
wait ×2
android ×1
c# ×1
loops ×1
sleep ×1
synchronize ×1
synchronized ×1
while-loop ×1