Guest User

Untitled

a guest
Apr 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. PrintServerV1.java:63: error: unreported exception InterruptedException; must be caught or declared to be thrown
  2. server.printRequest("homework7.txt");
  3. ^
  4. PrintServerV1.java:64: error: unreported exception InterruptedException; must be caught or declared to be thrown
  5. server.printRequest("assignment4.txt");
  6. ^
  7. PrintServerV1.java:65: error: unreported exception InterruptedException; must be caught or declared to be thrown
  8. server.printRequest("speech.pdf");
  9.  
  10. import java.util.*;
  11. import java.util.concurrent.locks.Lock;
  12. import java.util.concurrent.locks.ReentrantLock;
  13. import java.util.concurrent.locks.Condition;
  14.  
  15. import static java.lang.System.out;
  16.  
  17. public class PrintServerV1 implements Runnable {
  18.  
  19. private static final Queue<String> requests = new LinkedList<String>();
  20. private Lock lock = new ReentrantLock();
  21. private Condition condition = lock.newCondition();
  22.  
  23. public void printRequest(String s) throws InterruptedException {
  24. lock.lock();
  25. try {
  26. out.println("Adding print request for: " + s);
  27. requests.add(s);
  28. condition.signal();
  29. } finally { lock.unlock(); Thread.sleep(1000);}
  30.  
  31.  
  32. }
  33.  
  34. public void sendRequest() throws InterruptedException {
  35. lock.lock();
  36. try {
  37. while (requests.size() == 0) {
  38. condition.await();
  39. }
  40. out.println("Sending Request to printer");
  41. Thread.sleep(1000);
  42. while (!requests.isEmpty()) {
  43. realPrint(requests.remove());
  44. }
  45. } finally {
  46. lock.unlock();
  47. }
  48. }
  49.  
  50. private void realPrint(String s) throws InterruptedException {
  51. // do the real work of outputting the string to the screen
  52. out.println("Currently printing: " + s);
  53. Thread.sleep(1000);
  54. }
  55.  
  56. public void run() {
  57. try {
  58. sendRequest();
  59. } catch (InterruptedException exception) {}
  60. }
  61.  
  62. public static void main(String[] args) {
  63. PrintServerV1 server = new PrintServerV1();
  64. new Thread(server).start();
  65. server.printRequest("homework7.txt");
  66. server.printRequest("assignment4.txt");
  67. server.printRequest("speech.pdf");
  68. }
  69. }
Add Comment
Please, Sign In to add comment