Skip to content

Instantly share code, notes, and snippets.

@criskgl
Created July 12, 2019 12:13
Show Gist options
  • Save criskgl/c339b2acb8719e568429965ef6a7cbba to your computer and use it in GitHub Desktop.
Save criskgl/c339b2acb8719e568429965ef6a7cbba to your computer and use it in GitHub Desktop.
public static void main(String[] args){
//create threadpool
ExecutorService pool = Executors.newFixedThreadPool(iterations);
//create array of runnables that will be used to lunch each thread
myRunnable [] vec = new myRunnable [iterations];
//fill array of runnables
int iterations = 10;
for(int iter = 0; iter < iterations; iter++){
vec[iter] = new myRunnable(iter,iter+10);
}
/*10 threads will be created*/
/*Each thread will print its iteration number & its iteration number + 10*/
//tell our pool to start working using the vector filled with runnables
for(int iter = 0; iter < iterations; iter++){
pool.submit(vec[iter]);
}
}
class myRunnable implements Runnable{
myRunnable(int param1, int param2){
//here we save it when myRunnable is built
this.param1 = param1;
this.param2 = param2;
}
public void run(){
System.out.println(this.param1);
System.out.println(this.param2);
}//End of run method in Runnable
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment