So, I think I understand how fixed-thread pools work (using Executor.fixedThreadPool built into Java), but from what I see, the number of tasks you want to do is usually given, and you know how much until you run the program. for example
int numWorkers = Integer.parseInt(args[0]); int threadPoolSize = Integer.parseInt(args[1]); ExecutorService tpes = Executors.newFixedThreadPool(threadPoolSize); WorkerThread[] workers = new WorkerThread[numWorkers]; for (int i = 0; i < numWorkers; i++) { workers[i] = new WorkerThread(i); tpes.execute(workers[i]); }
Where every workerThread does something really simple, this part is arbitrary. I want to know that if you have a fixed pool size (say, 8 max.), But you don’t know how many workers you need to complete the task before execution.
A specific example: if I have a pool size of 8, and I read it from standard input. When I read, I break the input into blocks of a given size. Each of these blocks is assigned a stream (along with some other information) so that they can compress it. This way, I don't know how many threads I need to create, since I need to continue until I get to the end of the input. I also have to somehow make sure that the data remains in the same order. If thread 2 ends before thread 1 and just sends it to work, my data will be inoperative!
Would a thread pool be the wrong approach in this situation? It seems to be great (since I can't use more than 8 threads at a time).
Basically, I want to do something like this:
ExecutorService tpes = Executors.newFixedThreadPool(threadPoolSize); BufferedInputStream inBytes = new BufferedInputStream(System.in); byte[] buff = new byte[BLOCK_SIZE]; byte[] dict = new byte[DICT_SIZE]; WorkerThread worker; int bytesRead = 0; while((bytesRead = inBytes.read(buff)) != -1) { System.arraycopy(buff, BLOCK_SIZE-DICT_SIZE, dict, 0, DICT_SIZE); worker = new WorkerThread(buff, dict) tpes.execute(worker); }
This is not working code, I know, but I'm just trying to illustrate what I want.
I forgot a bit, but I'll see how the buff and dict values change, and I don't know how long the input is. I don’t think that I can’t actually make this thought, because a good worker already exists after the first call! I can’t just say work = new WorkerThread a lot of time, since it no longer points to an existing thread (true, a thread that may be dead) and, obviously, in this embodiment, if it works, I would not work in parallel. But I want to say that I want to continue to create threads until I get the maximum pool size, wait for the thread to finish, and then continue to create threads until I get to the end of the input.
I also need to keep things in order, which is the part that is really annoying.