Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. public static void main(String[] args){
  2. //create threadpool
  3. ExecutorService pool = Executors.newFixedThreadPool(iterations);
  4.  
  5. //create array of runnables that will be used to lunch each thread
  6. myRunnable [] vec = new myRunnable [iterations];
  7.  
  8. //fill array of runnables
  9. int iterations = 10;
  10. for(int iter = 0; iter < iterations; iter++){
  11. vec[iter] = new myRunnable(iter,iter+10);
  12. }
  13. /*10 threads will be created*/
  14. /*Each thread will print its iteration number & its iteration number + 10*/
  15.  
  16. //tell our pool to start working using the vector filled with runnables
  17. for(int iter = 0; iter < iterations; iter++){
  18. pool.submit(vec[iter]);
  19. }
  20. }
  21.  
  22. class myRunnable implements Runnable{
  23. myRunnable(int param1, int param2){
  24. //here we save it when myRunnable is built
  25. this.param1 = param1;
  26. this.param2 = param2;
  27. }
  28. public void run(){
  29. System.out.println(this.param1);
  30. System.out.println(this.param2);
  31. }//End of run method in Runnable
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement