title: Concurrency in Java - Delegating tasks to worker threads, am I doing it right?
body: <p>So I'm making an emulator for an online game and I can't seem to think of a good way to deal with lots of tasks running simultaneously. Loading everything on a single thread just doesn't work, obviously.</p>
  
  <p>My idea was to have a main thread that delegates tasks to x amount of worker threads. Once the main thread is done queueing tasks, it signals the workers to start firing tasks and halts untill they have finished. My implementation is as follows:</p>
  
  <pre><code>package com.rs2.engine;
  
  import java.util.concurrent.Executors;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.ScheduledExecutorService;
  import java.util.concurrent.TimeUnit;
  import java.util.concurrent.CountDownLatch;
  import java.util.ArrayList;
  
  import com.rs2.util.Timer;
  
  public class Engine implements Runnable {
  
      private ScheduledExecutorService scheduledExecutorService;
      private ExecutorService executorService;
      private int currentTick;
      private ArrayList&lt;Task&gt; tasks;
      private Timer timer;
  
      public Engine(int workers) {
          this.executorService = Executors.newFixedThreadPool(workers);
          this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
          this.currentTick = 0;
          this.tasks = new ArrayList&lt;&gt;(10000);
          this.timer = new Timer();
      }
  
      public int getCurrentTick() {
          return currentTick;
      }
  
      public ExecutorService getWorkers() {
          return executorService;
      }
  
      public void start() {
          this.scheduledExecutorService.scheduleAtFixedRate(this, 0, 600, TimeUnit.MILLISECONDS);
      }
  
      public void cycle() {
  
      }
  
      public void queueTask(Task task) {
          tasks.add(task);
      }
  
      public void processQueuedTasks() {
          try {
              CountDownLatch latch = new CountDownLatch(tasks.size());
              for (int i = 0; i &lt; tasks.size(); i++) {
                  Task t = tasks.get(i);
                  t.setCountDownLatch(latch);
                  executorService.submit(t);
              }
              latch.await();
              tasks.clear();
          } catch(Exception e) {
              e.printStackTrace();
          }
      }
  
      @Override
      public void run() {
          timer.reset();
          cycle();
          currentTick++;
          //System.out.println("Cycle time: "+timer.elapsed());
      }
  
  }
  </code></pre>
  
  <p><strong>queueTask()</strong> and <strong>processQueuedTasks()</strong> are only called from the main thread. This Engine is used to deal with any and all tasks that the server needs executing. First it processes networking events (incoming packets), then updating entities and other events. That's why I like to keep it abstract.</p>
  
  <p>Here's the Task class aswell:</p>
  
  <pre><code>package com.rs2.engine;
  
  import java.util.concurrent.CountDownLatch;
  
  public class Task implements Runnable {
  
      private CountDownLatch latch;
  
      public Task() {
      }
  
      @Override
      public void run() {
          execute();
          latch.countDown();
      }
  
      public void execute() {
  
      }
  
      public void setCountDownLatch(CountDownLatch latch) {
          this.latch = latch;
      }
  
  }
  </code></pre>
  
  <p>My questions are as follows:</p>
  
  <ol>
  <li>In the Engine class, is it OK to work with a regular ArrayList in terms of concurrency? </li>
  <li>Is there a better way of queueing tasks to the ExecutorService? I feel like it might cause issues if too many tasks are queued to it at the same time.</li>
  <li>Are there any engine frameworks I should be looking at before I start reinventing the wheel?</li>
  </ol>