0

I'm working on a Java-based program that reads from a file and sends each line into its own processing Runnable to do some single thread calculation. I'm using a fixed thread pool, one task for each usable core, to parallelize this. The file is huge and its not possible to load each one into memory when submitting jobs. Is it possible to have the main thread (that is submitting these tasks) pause until a thread in the thread pool becomes available?

Mike
  • 526
  • 7
  • 18
  • **Is it possible to have the main thread (that is submitting these tasks) pause until a thread in the thread pool becomes available** Its not even really possible for anyone to say anything without code?? – ShayHaned Jun 22 '17 at 15:51

1 Answers1

1

Create a simple thread pool with available worker thread same as pool size.before submitting check if available thread is there then submit else wait on lock.

Also you can use Semaphore which will block until the acquire() will get some value. Semaphore exanple:

Semaphore semaphore = new Semaphore(pool_size);

//critical section
semaphore.acquire();

...

semaphore.release();

Example of simple thread pool:

    private List<WorkerThread> workers;
        private LinkedList<WorkerThread> availWorkers = new LinkedList<WorkerThread>();
        private LinkedList<WorkerThread> busyWorkers = new LinkedList<WorkerThread>(); 

Submit method

   public boolean submit(Runnable runnable) {
            if (runnable == null) {
                return false;
            }

            synchronized (Lock) {

                handoffPending = true;

                // Wait until a worker thread is available
                while ((availWorkers.size() < 1) && !isShutdown) {
                    try {
                        Lock.wait(500);
                    } catch (InterruptedException ignore) {
                    }
                }

                if (!isShutdown) {
                    WorkerThread wt = (WorkerThread)availWorkers.removeFirst();
                    busyWorkers.add(wt);
                    wt.run(runnable);
                } else {
                    // If the thread pool is going down, execute the Runnable
                    // within a new additional worker thread (no thread from the pool).
                    WorkerThread wt = new WorkerThread(this, threadGroup,
                            "WorkerThread-LastJob", prio, isMakeThreadsDaemons(), runnable);
                    busyWorkers.add(wt);
                    workers.add(wt);
                    wt.start();
                }
                Lock.notifyAll();

            }

            return true;
        }
gati sahu
  • 2,576
  • 2
  • 10
  • 16