logo

PriorityThreadQueue and ListManager

Write a class called PriorityThreadQueue that manages a queue of threads with highest priority. Implement methods to check equality, shuffle elements, and swap values in two integer lists.

4 Pages670 Words24 Views
   

Added on  2023-03-17

About This Document

This document includes the implementation of PriorityThreadQueue and ListManager classes in Java. PriorityThreadQueue class demonstrates how to compare and sort threads based on their priority, while ListManager class shows how to shuffle and swap elements in two lists.

PriorityThreadQueue and ListManager

Write a class called PriorityThreadQueue that manages a queue of threads with highest priority. Implement methods to check equality, shuffle elements, and swap values in two integer lists.

   Added on 2023-03-17

ShareRelated Documents
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();
}
}
}
PriorityThreadQueue and ListManager_1
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
PriorityThreadQueue and ListManager_2

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
Shared Memory Segment Creation and Destruction in C Programming
|29
|2258
|453

IntListMethodsTest Class in Java
|2
|715
|415

Java Assignment Codes
|52
|5341
|240

Java Assignment Help | Online Homework
|13
|1219
|353

Memory Sequence Game - Java Program for Console Based Game
|8
|998
|259

Java Programming | Assignment
|9
|834
|20