XI. Consider the following classes. public class RunThreads public static void m
ID: 3580946 • Letter: X
Question
XI. Consider the following classes. public class RunThreads public static void main(stringl args) Executor Service executor Executors .newCachedThreadPool(): inti j numbers {0, 2, 4, 6, 8); inti j numbers2 (1, 3, 5, 7, 9); executor execute (new ThreadTORun (numbers 1)) executor execute (new ThreadToRun(numbers2)); executor shutdown public class ThreadToRun implements Runnable private int array; public ThreadToRun(inttj array) this array array public void run() for (int a t array) System.out.print (a); which of the following outputs are possible? possible 0123456789 impossible 2357014689 possible 1357902468 possible 1302547968 impossible 9876543210Explanation / Answer
Program:
import java.io.*;
public class RunThreads // Here we are Declaring class name as RunThreads.java
{
public static void main(String args[])
{
ExecutorService executor=Executors.newCachedThreadPool(); // Here we are creating object called executor
int[] numbers1={0,2,4,6,8};
// Here we are considering one dimensional array named as numbers1 and initializing array elements
int[] numbers2={1,3,5,7,9};
// Here we are considering one dimensional array named as numbers2 and initializing array elements
executor.execute(new ThreadToRun(numbers1)); //By using object called executor we are executing the elements in the array using ThreadToRun method
executor.execute(new ThreadToRun(numbers2)); //By using object called executor we are executing the elements in the array numbers2 using ThreadToRun method
executor.shutdown();// Here when both array elements are executed we are shutdown the ThreadToRun method
}
}
class ThreadToRun implements Runnable // Here we are considering another class called ThreadToRun which implements Runnable
{
private int[] array;
public ThreadToRun(int[] array) // Here we are writing Parameterized Constructor
{
this.array=array; // Here we are invoking values of array in current class
}
public void run() //Here we decalring the method called run
{
for(int a:array)
{
System.out.println(a); // Printing the values of a
}
}
}
Output:
In the above code Output will be all possible statements
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.