Rewrite the RunThreads and ThreadToRun classes so that the threads write their i
ID: 3570480 • Letter: R
Question
Rewrite the RunThreads and ThreadToRun classes so that the threads
write their integers to a common ArrayList of Integers synchronously. That is, the
ArrayList will contain all the integers passed to the threads with no omissions. Hint: use
the synchronized statement. This example shows what the synchronized statement looks
like, but some changes are needed to make it work in this problem:
ArrayList list = new ArrayList();
synchronized (list) {
list.add(13);
}
public class RunThreads {
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
int[] numbers1 = {0, 2, 4, 6, 8};
int[] numbers2 = {1, 3, 5, 7, 9};
executor.execute(new ThreadToRun(numbers1));
executor.execute(new ThreadToRun(numbers2));
executor.shutdown();
}
}
public class ThreadToRun implements Runnable {
private int[] array;
public ThreadToRun(int[] array) {
this.array = array;
}
public void run() {
for (int a : array) {
System.out.print(a);
}
}
}
Explanation / Answer
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class RunThreads
{
public static void main(String[] args)
{
ExecutorService executor = Executors.newCachedThreadPool();
int[] numbers1 = {0, 2, 4, 6, 8};
int[] numbers2 = {1, 3, 5, 7, 9};
System.out.println("First Array :");
print(numbers1);
System.out.println("Second Array :");
print(numbers2);
//It is not guaranteed that which thread of the pool gets
//the first priority to add integer elements .
//Output comes in two different versions.
//either first after second
//or second after first.
System.out.println("synchronily adding elements to the array list");
executor.execute(new ThreadToRun(numbers1));
executor.execute(new ThreadToRun(numbers2));
executor.shutdown();
}
public static void print(int values[])
{
for (int element : values)
{
System.out.print(element+" ");
}
System.out.println();
}
}
---------------------------------------------------------------------------------------------------------------------
//This class extends the Thread class rather than Runnable interface
//so that the elements are added to the ArrayList synchronously.
///ThreadToRun.java
import java.util.ArrayList;
import java.util.Arrays;
public class ThreadToRun extends Thread
{
private int[] array;
//Create an instance of ArrayList
private ArrayList<Object> list;
public ThreadToRun(int[] array)
{
this.array = array;
//create an instance of ArrayList with integer array
//as it argument
list=new ArrayList<Object>(Arrays.asList(array));
}
//Implement the run method of Thread class
//and synchronously added to the array list
//one after the other thread.
//Synchronized doesnot allow other thread
//unless the first thread completes adding
//elements to the first thread.
public void run()
{
for (int a : array)
{
synchronized (list)
{
//Add elements to the array list
list.add(a);
//print the element that is added to the array list.
System.out.print(a+" ");
}
}
}
}
-----------------------------------------------------------------------------------------------------------------------------
Sample output:
Possible output run1:
First Array :
0 2 4 6 8
Second Array :
1 3 5 7 9
synchronily adding elements to the array list
1 3 5 7 9 0 2 4 6 8
Possible output run2:
First Array :
0 2 4 6 8
Second Array :
1 3 5 7 9
synchronily adding elements to the array list
0 2 4 6 8 1 3 5 7 9
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.