svephoto

OS Planning [Java]

Jun 30th, 2021 (edited)
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class OSPlanning {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         ArrayDeque<Integer> tasks = new ArrayDeque<>();
  8.  
  9.         Arrays.stream(scanner.nextLine().split(", "))
  10.                 .mapToInt(Integer::parseInt)
  11.                 .forEach(tasks::push);
  12.  
  13.         ArrayDeque<Integer> threads = new ArrayDeque<>();
  14.  
  15.         Arrays.stream(scanner.nextLine().split(" "))
  16.                 .mapToInt(Integer::parseInt)
  17.                 .forEach(threads::offer);
  18.  
  19.         int taskToBeKilled = Integer.parseInt(scanner.nextLine());
  20.  
  21.         List<String> remainingThreads = new ArrayList<>();
  22.  
  23.         while (!threads.isEmpty() && !tasks.isEmpty()) {
  24.             int threadValue = threads.peek();
  25.             int taskValue = tasks.peek();
  26.  
  27.             if (taskValue == taskToBeKilled) {
  28.                 System.out.printf("Thread with value %d killed task %d%n", threadValue, taskToBeKilled);
  29.  
  30.                 for (Integer currentThread : threads) {
  31.                     remainingThreads.add(currentThread.toString());
  32.                 }
  33.                
  34.                 String finalResult = String.join(" ", remainingThreads);
  35.                
  36.                 System.out.printf("%s%n", finalResult);
  37.                 break;
  38.             }
  39.  
  40.             if (threadValue >= taskValue) {
  41.                 threads.poll();
  42.                 tasks.pop();
  43.             } else {
  44.                 threads.poll();
  45.             }
  46.         }
  47.     }
  48. }
  49.  
Add Comment
Please, Sign In to add comment