Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. public class Producer
  2. extends Thread
  3. {
  4. private volatile Storage store;
  5. private volatile Reader read;
  6.  
  7. Producer(Storage store, Reader read){
  8. this.read = read;
  9. this.store = store;
  10. }
  11.  
  12.  
  13.  
  14. public void run()
  15. {
  16. while (!read.isEmpty()) {
  17.  
  18. String FileName = read.returnAllPaths().peek().getFileName().toString();
  19. String item = null;
  20. try {
  21. item = read.returnAllPaths().take().toString();
  22. } catch (InterruptedException e1) {
  23. e1.printStackTrace();
  24. }
  25. File currentFile = new File(item);
  26. try (BufferedReader reader = new BufferedReader(new FileReader(currentFile))) {
  27. String line;
  28. while ((line = reader.readLine()) != null) {
  29. FileAndLine current = new FileAndLine(FileName, line);
  30. store.fillStore(current);
  31. }
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35.  
  36.  
  37. }
  38.  
  39. store.setEndOfPaths(true);
  40. }
  41. }
  42. public class Consumer
  43. extends Thread
  44. {
  45. private volatile Storage store;
  46. private String clue;
  47.  
  48. public Consumer(Storage store, String clue){
  49. this.store = store;
  50. this.clue = clue;
  51. }
  52.  
  53. public void run()
  54. {
  55.  
  56. FileAndLine currentLine;
  57.  
  58. while(!store.isEndOfPaths() || !store.isEmpty()){
  59. currentLine = store.depleteStore();
  60. System.out.println("q");
  61. if(currentLine.line.contains(clue))
  62. System.out.println(currentLine.FileName + ": " + currentLine.line);
  63. }
  64. }
  65.  
  66. }
  67. public class Storage {
  68. private BlockingQueue<FileAndLine> Store;
  69. private boolean full;
  70. private volatile boolean endOfPaths;
  71.  
  72.  
  73. public Storage(){
  74. Store = new LinkedBlockingQueue<FileAndLine>();
  75. full = false;
  76. }
  77.  
  78. private boolean isFull(){
  79. return full;
  80. }
  81.  
  82. public synchronized BlockingQueue<FileAndLine> getStore(){
  83. return this.Store;
  84. }
  85.  
  86. public synchronized boolean isEmpty(){
  87. return Store.isEmpty();
  88. }
  89.  
  90.  
  91. public synchronized void setEndOfPaths(boolean set){
  92. endOfPaths = set;
  93. }
  94.  
  95. public synchronized boolean isEndOfPaths(){
  96. return endOfPaths;
  97. }
  98.  
  99. public synchronized void fillStore(FileAndLine line){
  100. while(isFull()){
  101. try {
  102. wait();
  103. } catch (InterruptedException e) {
  104. e.printStackTrace();
  105. }
  106. }
  107. Store.add(line);
  108.  
  109. full = false;
  110. notifyAll();
  111.  
  112. if(Store.size() == 1000){
  113. full = true;
  114. }
  115. }
  116.  
  117. public synchronized FileAndLine depleteStore(){
  118.  
  119. FileAndLine line;
  120. if(endOfPaths == true && Store.isEmpty())
  121. {
  122. return new FileAndLine("", "");
  123. }
  124. while(Store.isEmpty())
  125. {
  126. try {
  127. wait();
  128. } catch (InterruptedException e) {
  129. e.printStackTrace();
  130. }
  131. }
  132. line = new FileAndLine(Store.remove());
  133.  
  134. if(Store.size() < 1000){
  135. full = false;
  136. notifyAll();
  137. }
  138. return line;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement