Concurrent Programming Assignment Solution - Java Threads and Lists

Verified

Added on  2023/03/17

|4
|670
|24
Homework Assignment
AI Summary
This assignment solution addresses two concurrent programming problems in Java. The first problem involves creating a `PriorityThreadQueue` class that manages a queue of threads, ensuring that threads with the highest priority are executed first. The solution includes a `ThreadSorting` comparator to sort threads based on their priority and a `main` method to test the implementation by randomly assigning priorities to threads and demonstrating their execution order. The second problem involves a `ListManager` class that manages two integer lists and handles concurrent access. The solution implements methods for shuffling elements in both lists (`shuffleBoth`), swapping elements atomically (`atomicSwap`), and checking for equality between corresponding elements (`bothEqual`). The `main` method tests the `ListManager` class by creating instances, running its methods, and printing the results, showcasing concurrent list manipulation and thread management.
Document Page
1. Questions 1.
import java.util.Comparator;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Random;
public class PriorityThreadQueue extends Thread implements Runnable
{
public void run()//implementing the run method for each Thread
{
System.out.println("This is currently the thread with the highest priority "
+ this.getPriority());
}
//To compare the priority of the thread to neccesiate the storage
//of one with the highest priority.
static class ThreadSorting implements Comparator<PriorityThreadQueue>
{
@Override
public int compare(PriorityThreadQueue t1, PriorityThreadQueue t2) {
if(t1.getPriority() > t2.getPriority())
return 1;
else if(t1.getPriority() == t2.getPriority())
return 0;
else
return -1;
}
}
//Driver program to randomly assign priorities to threads
//It tests their priorities.
public static void main(String[] args) {
//Lets create the threads
PriorityThreadQueue t1 = new PriorityThreadQueue();
PriorityThreadQueue t2 = new PriorityThreadQueue();
PriorityThreadQueue t3 = new PriorityThreadQueue();
Random rnd = new Random();
//Lets randomly generate priorities for the threads
//throws IllegalArgument exception otherwise
t1.setPriority(rnd.nextInt(10));
t2.setPriority(rnd.nextInt(10));
t3.setPriority(rnd.nextInt(10));
List<PriorityThreadQueue> list = new ArrayList<>();
list.add(t1);
list.add(t2);
list.add(t3);
Collections.sort(list, new ThreadSorting());//sorting the threads based on our
//ThreadSorting class
//Test the threads stored in the arraylist
for(int i = list.size() -1; i > 0; i--)
{
PriorityThreadQueue curr = list.get(i);
curr.run();
}
}
}
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
A screenshot illustrating the result of the above class.
2. Question 2.
import java.util.*;
public class ListManager implements Runnable
{
int i;
int k;
private List<Integer> lst1 = new ArrayList<>();
private List<Integer> lst2 = new ArrayList<>();
public ListManager(int n,int i,int k ){
this.i =i;
this.k = k;
for(int j = 0; j < n; j++){
lst1.add((int)(Math.random()*100));
lst2.add((int)(Math.random()*100));
}
}
@Override
public void run() {
//To shuffleBoth
shuffleBoth();
//To atomicSwap the lists
atomicSwap(i, k);
//To check if they are equal
Document Page
boolean equal = bothEqual();
if(equal)
System.out.println("They are both equal");
}
private void atomicSwap(int i, int j)
{
try {
Collections.swap(this.lst1, 0,i);
Collections.swap(this.lst2, 0, j);
}
catch(Exception e) {
e.printStackTrace();
}
}
private void shuffleBoth() {
Collections.shuffle(lst1);
Collections.shuffle(lst2);
}
private boolean bothEqual()
{
Random rand = new Random();
try {
int i = rand.nextInt(lst1.size());
if (lst1.get(i) == lst2.get(i))
return true;
}catch(Exception e)
{
e.printStackTrace();
}
return false;
}
//Driver program to test the program.
Public static void main (String[] args)
{
int N = 8;
int i = 1;
int j = 2;
ListManager manager = new ListManager(N,i, j);
manager.run();
}
}
Document Page
A screenshot illustrating the results of the program 2.
chevron_up_icon
1 out of 4
circle_padding
hide_on_mobile
zoom_out_icon