Posted By

jerryvig on 09/13/11


Tagged

java simple thread Multithreading pool


Versions (?)

Simple Java Multithreading Example


 / Published in: Java
 

This is a simple Java multithreading example that uses ActualThread as the class in which the thread code is contained. ActualThread must implement runnable.

  1. ExecutorService executor = Executors.newFixedThreadPool(threadAmount);
  2. List<Callable<Object>> tasks = new ArrayList<Callable<Object>>();
  3.  
  4. for (int i = 1; i < 50; i++) {
  5. tasks.add(Executors.callable(new ActualThread(i)));
  6. }
  7. executor.invokeAll(tasks);
  8. executor.shutdown(); // not really necessary if the executor goes out of scope.
  9. System.out.println("Finished all threads");

Report this snippet  

You need to login to post a comment.