Ladies_Man

BG-Multithreading v1

Jul 19th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.24 KB | None | 0 0
  1. package ru.belyaev.multithreading;
  2.  
  3. import ru.belyaev.multithreading.queue.IThreadSafeQueue;
  4. import ru.belyaev.multithreading.queue.ThreadSafePriorQueue;
  5. import ru.belyaev.multithreading.thread.ReaderThread;
  6. import ru.belyaev.multithreading.thread.WriterThread;
  7.  
  8. public class ThreadRunner {
  9.  
  10.     private ReaderThread reader;
  11.     private WriterThread writer;
  12.  
  13.     ThreadRunner(long readerExecutionInterval) {
  14.         final IThreadSafeQueue<Integer> queue = new ThreadSafePriorQueue<Integer>(Integer::compareTo);
  15.         reader = new ReaderThread(queue, readerExecutionInterval);
  16.         writer = new WriterThread(this, queue);
  17.     }
  18.  
  19.     private void start() {
  20.         reader.start();
  21.         writer.start();
  22.  
  23.         System.out.println("Program has started");
  24.     }
  25.  
  26.     public void stop() {
  27.         System.out.println("Stopping program");
  28.  
  29.         reader.interrupt();
  30.         writer.interrupt();
  31.     }
  32.  
  33.     public static void main(String[] args) {
  34.         long readerExecutionInterval = 3000;
  35.  
  36.         new ThreadRunner(readerExecutionInterval).start();
  37.     }
  38. }
  39.  
  40.  
  41.  
  42.  
  43.  
  44. package ru.belyaev.multithreading.thread;
  45.  
  46. import ru.belyaev.multithreading.queue.IThreadSafeQueue;
  47.  
  48. /**
  49.  * Created by ABelyaev on 19.07.2017.
  50.  */
  51. public class ReaderThread extends Thread{
  52.  
  53.     private final IThreadSafeQueue queue;
  54.     private long executionInterval;
  55.  
  56.     public ReaderThread(IThreadSafeQueue queue, long executionInterval) {
  57.         this.queue = queue;
  58.         this.executionInterval = executionInterval;
  59.     }
  60.  
  61.     @Override
  62.     public void run() {
  63.         while (!Thread.currentThread().isInterrupted()) {
  64.             try {
  65.                 Integer x = (Integer)queue.removeMin();
  66.                 if (null != x) {
  67.                     System.out.println(x + " was removed from queue: " + queue);
  68.                 }
  69.                 sleep(executionInterval);
  70.  
  71.             } catch (InterruptedException e) {
  72.                 return;
  73.             }
  74.         }
  75.     }
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82. package ru.belyaev.multithreading.thread;
  83.  
  84. import ru.belyaev.multithreading.ThreadRunner;
  85. import ru.belyaev.multithreading.queue.IThreadSafeQueue;
  86.  
  87. import java.util.Scanner;
  88.  
  89. /**
  90.  * Created by ABelyaev on 19.07.2017.
  91.  */
  92. public class WriterThread extends Thread {
  93.  
  94.     private final IThreadSafeQueue<Integer> queue;
  95.     private ThreadRunner runner;
  96.  
  97.     private final String CMD_STOP = "stop";
  98.  
  99.  
  100.     public WriterThread(ThreadRunner runner, IThreadSafeQueue<Integer> queue) {
  101.         this.runner = runner;
  102.         this.queue = queue;
  103.     }
  104.  
  105.     @Override
  106.     public void run() {
  107.         while (!interrupted()) {
  108.             Scanner scanner = new Scanner(System.in);
  109.             String readString;
  110.  
  111.             while (null != (readString = scanner.nextLine()) && !Thread.currentThread().isInterrupted()) {
  112.  
  113.                 if (!readString.isEmpty()) {
  114.                     if (CMD_STOP.equalsIgnoreCase(readString)) {
  115.                         runner.stop();
  116.                     } else {
  117.                         try {
  118.                             int x = Integer.valueOf(readString);
  119.                             System.out.println(x + (queue.add(x) ? "was added" : "was not added"));
  120.                             System.out.println("current queue: " + queue);
  121.  
  122.                         } catch (NumberFormatException e) {
  123.                             System.out.println("Provided value is not a valid number");
  124.                         }
  125.                     }
  126.                 }
  127.             }
  128.         }
  129.     }
  130. }
  131.  
  132.  
  133.  
  134.  
  135.  
  136. package ru.belyaev.multithreading.queue;
  137.  
  138. import java.util.Comparator;
  139. import java.util.PriorityQueue;
  140.  
  141. /**
  142.  * Created by ABelyaev on 19.07.2017.
  143.  */
  144. public class ThreadSafePriorQueue<Number>  implements IThreadSafeQueue<Number> {
  145.  
  146.     private final PriorityQueue<Number> queue;
  147.  
  148.     public ThreadSafePriorQueue(Comparator<? super Number> comparator) {
  149.         queue = new PriorityQueue<>(comparator);
  150.     }
  151.  
  152.     public boolean add(Number e) {
  153.         synchronized (this) {
  154.             return queue.add(e);
  155.         }
  156.     }
  157.  
  158.     public Number removeMin() {
  159.         synchronized (this) {
  160.             return queue.poll();
  161.         }
  162.     }
  163.  
  164.     @Override
  165.     public String toString() {
  166.         return queue.toString();
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment