d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Simple Java Multithreading Sync Question > (probably)
Add Reply New Topic New Poll
Member
Posts: 27,804
Joined: Jul 11 2006
Gold: 1,970.00
Mar 30 2013 08:58pm
I have a really complicated program going on, but this is the gist of it. You can assume everything else is going alright. I just need a bit of guidance in choosing a proper synchronization structure.

Code
while (!done) {
 if (!workQueue.isEmpty() && threadsRunning < THREAD_RUNNING_THRESHOLD)
   Spawn new thread to do work;
   threadsRunning++;
 else if (workQueue.isEmpty()) //This is for handling final thread. Since an error can occur during work, it needs to wait to make sure the final thread succeeded.
   //Some kind of structure to wait here for thread to notify final thread finished. done should have been set to true in the thread if
   //it was the final thread and it succeeded, so that will get us out of this loop also)
 else //if (too many threads running already)
   //Some kind of structure to wait here for thread to notify a thread finished
}


So previously I used CountDownLatch, but I'm not using that in this case because there's no way to reset a CountDownLatch.
I've previously used a Lock and Condition when I wrote my own multithread save Queue class, but I don't really understand what I used them for there (it was an assignment in the class and that part of the code was given to us), so I'm not really sure what all would be appropriate here.

I'm probably doing stuff ridiculously more complicated compared to this in my actual code, yet I don't have such a (probably) simple basic down. Dx

This post was edited by Godel on Mar 30 2013 09:04pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 31 2013 09:00am
I'm not sure what you're talking about. You're saying sync and workqueue as though we should know what that means to you. Either you're being too general for a very specific problem or you're being too specific for a general problem (eg: how to tell if all spawned threads are done).

I'm going to assume the latter:

http://stackoverflow.com/questions/702415/how-to-know-if-other-threads-have-finished
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 31 2013 12:23pm
Quote (carteblanche @ Mar 31 2013 11:00am)
I'm not sure what you're talking about. You're saying sync and workqueue as though we should know what that means to you. Either you're being too general for a very specific problem or you're being too specific for a general problem (eg: how to tell if all spawned threads are done).

I'm going to assume the latter:

http://stackoverflow.com/questions/702415/how-to-know-if-other-threads-have-finished


if its the later as well windows api has a function

Code
DWORD WINAPI WaitForMultipleObjects(
 _In_  DWORD nCount,
 _In_  const HANDLE *lpHandles,
 _In_  BOOL bWaitAll,
 _In_  DWORD dwMilliseconds
);


This post was edited by AbDuCt on Mar 31 2013 12:25pm
Member
Posts: 27,804
Joined: Jul 11 2006
Gold: 1,970.00
Mar 31 2013 02:12pm
Quote (carteblanche @ Mar 31 2013 11:00am)
I'm not sure what you're talking about. You're saying sync and workqueue as though we should know what that means to you. Either you're being too general for a very specific problem or you're being too specific for a general problem (eg: how to tell if all spawned threads are done).

I'm going to assume the latter:

http://stackoverflow.com/questions/702415/how-to-know-if-other-threads-have-finished


Sorry it was unspecific. Basically I have a work queue that holds computationally intensive queue. Each loop iteration removes from the top of the queue and gives it to a new thread to finish.
However, I want to limit the number of threads that run at the same time, so I want to prevent the thread spawner from continuing until AT LEAST ONE thread finishes and only let it continue after a thread finishes. So if !(threadsRunning < THREAD_RUNNING_THRESHOLD) then sit there and wait until a thread says it's done.

It looks like I'm gonig to want to use a Condition for that, but that also is related to Lock, and for what I'm doing, a lock isn't needed. (Or maybe it is and I have no clue what I'm doing? Maybe! Dx)
But really I'm not sure what structure specifically is desirable for this. I'm pretty sure I want to use something in java.util.concurrent, but I'm very unknowledgable with those, so I'm just lost :(
Basically something like an event listener would suffice, but I don't know what structure has something like that.

This post was edited by Godel on Mar 31 2013 02:17pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 31 2013 02:24pm
Creating new threads for something that occurs so often doesn't sound too great. Look into thread pools.

http://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html
Member
Posts: 27,804
Joined: Jul 11 2006
Gold: 1,970.00
Mar 31 2013 03:24pm
I ended up using a synchronized (this) block in my main class and had and had wait() and put synchronized (spawner) blocks in my thread class to notify.

Quote (carteblanche @ Mar 31 2013 04:24pm)
Creating new threads for something that occurs so often doesn't sound too great. Look into thread pools.

http://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html


I appreciate the input, but that really wasn't my question.
Although I'd like to make stuff as good as I can, implementing structures I'm not used to takes a lot of time and effort :\ if I feel like I have more time later, I might go back and change my spammed (new Thread(...)).start();s into something more elegant.

Performance wise, I did care enough that I'm no longer putting while (condition) {//empty loop}, which is what I used in the last version of this program, but upgrading it to optimal perfection is going to require far more time and effort than I want, OR NEED, to spend on it right now, to be honest :(

This post was edited by Godel on Mar 31 2013 03:30pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 31 2013 03:30pm
i like quoting people on stackoverflow when ever i hear about people wanting to use threading. "most of the time when a user wants to add threading they do not actually need it"

this holds through to almost every threading post i read.
Member
Posts: 27,804
Joined: Jul 11 2006
Gold: 1,970.00
Mar 31 2013 03:40pm
Quote (AbDuCt @ Mar 31 2013 05:30pm)
i like quoting people on stackoverflow when ever i hear about people wanting to use threading. "most of the time when a user wants to add threading they do not actually need it"

this holds through to almost every threading post i read.


I hope you weren't referring to this post, because I definitely didn't give enough information on what I'm doing for you to be able to say that.

If you must know, I'm programming a cluster storage program that can download pieces from N clients of N files and distribute those pieces to N nodes. Parallelizing it is essential!
I can't make it perfect the first time around, especially with things I'm not used to using. I'm creating this for a learning process; I'm not going to sell this to a company or something :p

"Probably doing it wrong but it works so whatever" is maybe a very wrong attitude to approach it, but I'm not doing this professionally, thankfully.

This post was edited by Godel on Mar 31 2013 03:56pm
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Apr 2 2013 05:47am
Why can't you just make a new CountDownLatch when you need it? Why does it need to be able to reset the instance?
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll