Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement