[英]Locking threads in java doesnt work
I have a simple problem with multithreading that I just cant figure out. 我对多线程有一个简单的问题,我无法解决。 I tried synchronizing methods and using locks but none of these worked so I hope someone here could help me.
我尝试了同步方法并使用锁,但是这些方法均无效,因此希望这里有人可以帮助我。
public void startThreads(ArrayList<ArrayList<Tocka>> pLista) {
ScheduledThreadPoolExecutor eventPool = new ScheduledThreadPoolExecutor(pLista.size());
for(int i=0; i<pLista.size(); i++) {
eventPool.execute(new Dretva(pLista.get(i),i));
}
}
public synchronized void run() {
lock.lock();
System.out.print(this.id + ". ");
optimalnost.printOrder(this.lista);
double distance = optimalnost.getDistanceList(this.lista);
System.out.println(" - " + distance);
System.out.println();
checkMin(distance);
lock.unlock();
}
I call from test class startThreads method with List of lists(startThreads and thread are in separated classes). 我从带有列表列表的测试类startThreads方法调用(startThreads和线程在单独的类中)。 In class Dretva (thread), I have a run code which executes some calculations with list which is passed as argument and prints calculation.
在类Dretva(线程)中,我有一个运行代码,该代码执行带有列表的一些计算,该列表作为参数传递并打印计算。 In my example there are 6 lists and I'm trying to pass all of them, but one at the time.
在我的示例中,有6个列表,而我试图通过所有列表,但当时只有一个。 When I don't use threads it works fine, but with threads like these i get messed up and shuffled output.
当我不使用线程时,它可以正常工作,但是使用此类线程时,我会陷入混乱并混排输出。
I need help how to make other threads wait while active thread isn't finished. 我需要帮助,以便在活动线程未完成时如何使其他线程等待。
Here is screenshot of my problem: 这是我的问题的屏幕截图:
The synchronized method is synchronized on that object, not globally. 同步的方法在该对象上同步,而不是全局同步。
Just pass in an object and synchronize on that: 只需传入一个对象并对其进行同步:
public void startThreads(ArrayList<ArrayList<Tocka>> pLista) {
final Object lockOb = new Object();
ScheduledThreadPoolExecutor eventPool = new ScheduledThreadPoolExecutor(pLista.size());
for(int i=0; i<pLista.size(); i++) {
eventPool.execute(new Dretva(lockOb, pLista.get(i),i));
}
}
Object lockOb;
public void run() {
synchronized(lockOb) {
System.out.print(this.id + ". ");
optimalnost.printOrder(this.lista);
double distance = optimalnost.getDistanceList(this.lista);
System.out.println(" - " + distance);
System.out.println();
checkMin(distance);
}
}
Now all the objects are sharing one lock object and are all synchronizing on that, so only one will run at a time. 现在,所有对象都共享一个锁对象,并且都在该对象上进行同步,因此一次只能运行一个。 This does entirely defeat the point of using threads though as you have created lots of threads, then deliberately made it so only one of those threads can execute at a time.
尽管您创建了很多线程,然后故意使它成为一个线程,所以一次只能执行其中的一个线程,但这确实完全使用了线程。
As a side note you should always use a try finally
block for any sort of lock object that needs unlocking. 附带说明
try finally
,对于需要解锁的任何锁定对象,都应始终使用try finally
块。
Your synchronized method run
is a member method. 您的同步方法
run
是成员方法。 This means that you are synchronizing on the instance of the object. 这意味着您正在对象的实例上进行同步。 Since you created multiple instances, each is obtaining a lock on a different object.
由于您创建了多个实例,因此每个实例都获得了对不同对象的锁定。 If you desire to synchronize access across instances, you must either explicitly provide the mutex to lock on or utilize a static constant mutex for all instances to synchronize on.
如果希望跨实例同步访问,则必须显式提供互斥锁以锁定或对所有实例使用静态常量互斥锁进行同步。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.