Advertisement
Guest User

Untitled

a guest
May 5th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Queue;
  3. import java.util.concurrent.BlockingQueue;
  4. import java.util.concurrent.LinkedBlockingDeque;
  5. import java.util.concurrent.ThreadLocalRandom;
  6.  
  7. public class ProducerConsumerTest {
  8.  
  9. public static void main(String[] args) {
  10. Container c = new Container();
  11. Producer p1 = new Producer(c);
  12. Consumer c1 = new Consumer(c);
  13. p1.start();
  14. c1.start();
  15. }
  16.  
  17. }
  18.  
  19. class Container {
  20.  
  21. private BlockingQueue<Item> content;
  22. private boolean isEnd;
  23.  
  24. public Container() {
  25. content = new LinkedBlockingDeque<>();
  26. isEnd = false;
  27. }
  28.  
  29. public synchronized Queue<Item> content() {
  30. return content;
  31. }
  32.  
  33. public synchronized boolean isEnd() {
  34. return isEnd;
  35. }
  36.  
  37. public synchronized void setEnd(boolean end) {
  38. isEnd = end;
  39. }
  40.  
  41. }
  42.  
  43. class Consumer extends Thread {
  44.  
  45. private Container container;
  46. private int size;
  47. private int count;
  48.  
  49. public Consumer(Container container) {
  50. this.container = container;
  51. this.count = 0;
  52. this.size = 0;
  53. }
  54.  
  55. public void run() {
  56. while (!container.isEnd() || !container.content().isEmpty()) {
  57. synchronized (container.content()) {
  58. if (!container.content().isEmpty()) {
  59. size += container.content().poll().weight;
  60. count++;
  61. if (count % 10 == 0) {
  62. System.out.println("Consumer readed " + count + " elements. Total size: " + size);
  63. }
  64. }
  65. }
  66. }
  67. System.out.println("Consumer readed " + count + " elements. Total size: " + size);
  68. }
  69. }
  70.  
  71. class Producer extends Thread {
  72.  
  73. private Container container;
  74. private int count;
  75.  
  76. public Producer(Container container) {
  77. this.container = container;
  78. this.count = 0;
  79. }
  80.  
  81. public void run() {
  82. try (BufferedReader br = new BufferedReader(new FileReader("towary.txt"))) {
  83. String line;
  84. while ((line = br.readLine()) != null) {
  85. String[] s = line.split("\\s");
  86. Item item = new Item(Integer.valueOf(s[0]), Integer.valueOf(s[1]));
  87. container.content().add(item);
  88. count++;
  89. if (count % 5 == 0) {
  90. //System.out.println("Producer putted " + count + " elements.");
  91. }
  92. }
  93. br.close();
  94. } catch (IOException e) {
  95. e.printStackTrace();
  96. }
  97. System.out.println("Producer putted " + count + " elements.");
  98. container.setEnd(true);
  99. }
  100.  
  101. }
  102.  
  103. class Item {
  104.  
  105. protected int id;
  106. protected int weight;
  107.  
  108. public Item(int id, int weight) {
  109. this.id = id;
  110. this.weight = weight;
  111. }
  112.  
  113. @Override
  114. public String toString() {
  115. return "ITEM(" + id + "," + weight + ")";
  116. }
  117.  
  118. }
  119.  
  120. class Generator {
  121.  
  122. public static void main(String[] args) {
  123. try (BufferedWriter bw = new BufferedWriter(new FileWriter("towary.txt", false))) {
  124. for (int i = 0; i < 10000; i++) {
  125. bw.write(i + " " + ThreadLocalRandom.current().nextInt(0, 1000));
  126. bw.newLine();
  127. }
  128. bw.flush();
  129. bw.close();
  130. } catch (IOException e) {
  131. e.printStackTrace();
  132. }
  133. }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement