简体   繁体   English

如何同步使用多个线程?

[英]how to use multiple threads, in sync?

i have multiple threads, who all run from one object. 我有多个线程,它们都从一个对象运行。 i want the "main thread" to run alone until a certain point, then it waits and all the other threads run together, then the main thread wakes, etc..... i am having trouble synchronizing my threads. 我希望“主线程”单独运行到某个点,然后等待,所有其他线程一起运行,然后主线程唤醒,等等。。。我在同步线程时遇到问题。 i either get an Illegal Monitor State Exception, or it gets stuck in one of the "wait" loops that are suppose to receive a "notify" that never arrives. 我或者得到了一个非法的监视器状态异常,或者它被卡在了一个“等待”循环中,该循环应该接收到永远不会到达的“通知”。

more specifically, i have an object with an array. 更具体地说,我有一个带有数组的对象。 each cell in the array has a thread that checks the adjacent cells and then changes it's cell with that information. 数组中的每个单元都有一个线程,该线程检查相邻的单元,然后使用该信息更改其单元。 in order to make the changes orderly, i want all the cells to first make the check of their adjacent cells and keep the value they produced, then wait. 为了有序地进行更改,我希望所有单元格首先检查其相邻单元格并保留它们产生的值,然后等待。 when all of them are done, the main thread will wake all of them up and they will update their respective cells. 完成所有操作后,主线程将唤醒所有操作,并更新各自的单元。

i looked up how "wait" and "notify" work, but i still don't understand how they sync. 我查了一下“等待”和“通知”的工作方式,但是我仍然不明白它们是如何同步的。 from what i understand i need to connect them all to one object, and then that object is the "lock", so if i use "synchronize" on its methods only one thread can approach it at a time? 据我了解,我需要将它们全部连接到一个对象,然后该对象就是“锁”,所以如果我在其方法上使用“同步”,一次只能有一个线程来接近它? how can i make sure a "wait" method will always have a "notify" to end it? 我如何确保“等待”方法始终带有“通知”来结束它?

Edit: the method basically runs Conway's game of life. 编辑:该方法基本上运行了Conway的生活游戏。 the main orientation of the code is like so: the class LifeMatrix extends JPanel. 代码的主要方向是这样的:LifeMatrix类扩展了JPanel。 it had an array of panels, each is either "dead or alive" (true/false). 它有一组面板,每个面板都是“死的或活着的”(是/否)。 the class RunMatrixThread extends thread, and is the "main thread" that coordinates the code. RunMatrixThread类扩展线程,并且是协调代码的“主线程”。 the class CellThead extends thread, and a CellThread is made for every cell in the matrix. 类CellThead扩展线程,并为矩阵中的每个单元格创建一个CellThread。 so my idea was to give all the threads the "LifeMatrix" as an observer, but if i try to notify the LifeMatrix Object (with matrix.notify()) it gives me the Illigal Monitor State Exception, and if i try to use "notify all" it gets stuck in RunMatrixThread's wait() command. 因此,我的想法是为所有线程提供“ LifeMatrix”作为观察者,但是,如果我尝试通过LifeSizeObject通知LifeMatrix对象(带有matrix.notify()),它将给我Illigal Monitor State Exception,并且如果我尝试使用“通知所有”它被卡在RunMatrixThread的wait()命令中。 also, do i notify an object? 另外,我会通知对象吗? or do i notify the threads that are waiting? 还是我通知正在等待的线程?

Don't use parallelization. 不要使用并行化。 Before using threads think if you really can parallelize your job because if all of your tasks have to be sync with each other use threads won't give you better perfomance in terms of execution time. 在使用线程之前,请考虑一下您是否真的可以并行化工作,因为如果必须将所有任务彼此同步,则使用线程不会在执行时间方面为您提供更好的性能。 Say that you have an array of objects [a,b] if a must waiting for some changes on b, you can't treat a and b separately so you can't parallelize your job. 假设您有一组对象[a,b],如果a必须等待b上的某些更改,则不能将a和b分开对待,因此无法并行化工作。 On the contrary if you need to process a, b and all the elements of your array and at the end perform some computation on them you can Join the threads with join() method. 相反,如果您需要处理a,b和数组的所有元素,最后对它们执行一些计算,则可以使用join()方法加入线程。 When you call join method you basically join threads branches in one (the main thread). 调用join方法时,基本上是将线程分支合并为一个(主线程)。 A new thread will fork your main thread and join will join these threads. 一个新线程将派生您的主线程,而join将加入这些线程。

If you're trying to get "worker threads" to do parcels of work that are authorized/initiated/doled-out by a "main" thread, then you probably should be using a thread pool (eg, https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadPoolExecutor.html ) 如果您试图让“工作线程”执行由“主”线程授权/启动/删除的工作,则您可能应该使用线程池 (例如https:// docs。 oracle.com/javase/8/docs/api/java/util/concurrent/ThreadPoolExecutor.html

A thread pool takes care of creating the worker threads and "synchronizing" their activity with the main thread, and it lets you focus on the task (or tasks) that the workers perform. 线程池负责创建工作线程并将其活动与主线程“同步”,并让您专注于工作线程执行的任务。


each cell in the array has a thread that... 数组中的每个单元格都有一个线程...

For Conway's Life, that's way too many worker threads. 对于Conway的一生来说,那就是太多的工作线程。 If the work is purely compute-bound, then there's no point in having many more threads than your host has processors to execute them. 如果工作纯粹是受计算限制的,那么没有更多的线程比主机拥有处理器来执行它们的线程没有意义。

If I was coding life for a host with N processors, I would use a thread pool that had N threads. 如果要为具有N个处理器的主机编码生命,我将使用具有N个线程的线程池。 And, In each generation, I would have the main thread submit N tasks to the pool: Each task would do one horizontal stripe of the board. 而且,在每一代中,我都会让主线程向池中提交N个任务:每个任务都将在电路板上做一个水平条纹。

Ok, first of all i want to thank all of you for trying to help, some of the links you gave me were very helpful. 好的,首先,我要感谢大家的帮助,您提供给我的一些链接非常有帮助。

I found out what my problem was: i was trying to use wait/notify methods from 2 different types of threads at the same time, on the same object. 我发现了我的问题所在:我试图同时在同一对象上使用来自2种不同类型的线程的wait / notify方法。 i had the 'CellThread' that used wait and 'notifyAll', and i had the 'RunMatrixThread' that did the same. 我有使用“等待”和“ notifyAll”的“ CellThread”,而我也有“ RunMatrixThread”。 they of course had "synchronized" methods, but because they were 2 different TYPES of threads, the two types weren't in sync with EACH OTHER. 它们当然具有“同步”方法,但是由于它们是2种不同的线程类型,因此这两种类型与每个线程都不同步。 what solved the problem was that i made 2 new synchronized methods within the 'RunMatrixThread' class, one for waiting and one for notifying, and then just called those methods from all threads (from both thread classes) whenever i wanted to wait/notify. 解决问题的方法是,我在“ RunMatrixThread”类中创建了2个新的同步方法,一个用于等待,一个用于通知,然后只要我想等待/通知,就从所有线程(两个线程类)中调用这些方法。 in this way, there was a unified object that had a lock on everything. 这样,就有了一个统一的对象,它可以锁定所有内容。

PS: i know its a bad idea to use so many threads. PS:我知道使用这么多线程是个坏主意。 it was the coarse's assignment, and they required we do it this way. 这是粗选作业,他们要求我们以这种方式进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM