2

I need to process 80 files of info, and I'm doing it via groups of 8 threads, what i would like to do is to to always have 8 threads running (right now i have 8 threads, and after those 8 finish their job, another 8 are generated, and so on).

So I Would like to know if there is a way to do his:

  • launch 8 threads.
  • after 1 thread finishes its job, launch another thread (so all the time I have 8 threads running, until the job is done)
6
  • 8
    Use a ThreadPool. Check out the ThreadPoolExecutor API. Commented Jul 9, 2013 at 22:51
  • @HovercraftFullOfEels That may be the single shortest useful answer I've seen here. Commented Jul 9, 2013 at 22:52
  • eclipse has a launcher it executes any job. Commented Jul 9, 2013 at 22:53
  • 1
    @CPerkins: I added an API link. ;) Commented Jul 9, 2013 at 22:53
  • instead of finish and relaunch threads, they can be programmed to pickup next file job from shared queue. Commented Jul 10, 2013 at 6:48

2 Answers 2

5

Why not use a thread pool, and in particular a fixed size thread pool ? Configure your thread pool size to be 8 threads, and then submit all your work items as Runnable/Callable objects. The thread pool will execute these using the 8 configured threads.

Sign up to request clarification or add additional context in comments.

Comments

3

So, everyone is quick to jump in and tell you to use a thread pool. Sure, that's the right way to achieve what you want. The question is, is it the right thing to want? It's not as simple as throw a bunch of threads at the problem, and magically everything is solved.

You haven't told us the nature of the processing. Are the jobs I/O bound, or CPU bound1? If they are CPU bound, the threads do nothing. If they are I/O bound, the threading might help.

You haven't told us if you have eight cores (or compute units). If you can't guarantee that you'll have that, it might not be best to have eight threads running.

There's a lot to think about. You're increasing the complexity of your solution. Maybe it's getting you what you want, maybe not.

1: Yes, you said you're processing files, but that doesn't tell us enough. Maybe the processing is intensive (think: rendering a video file). Or maybe you're reading the files from a very fast disk (think: SSD or memory-mapped files).

2 Comments

+ 1 from me: Excellent points, and I hope the OP will bear them in mind for later in his/her career. For now, though, the specificity of the request (exactly 80 files and 8 threads) sounds like an assignment, doesn't it?
Well the server has 4 cores, and the 8 threads come from trial/error on performance with the database, as i escalated the threads, thats the number that gave me the best performance, the processing its just about reading the file from start to end, do some Checking against the database info and inserting the récords on a table, disks on server are sataIII. So yeah im trying New things at work and this is something that could be considered an assignment. So its a Good idea to use the threads? Hope you can help me on this

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.