Guest User

Untitled

a guest
Sep 23rd, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. public class ticketServer {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. ConcurrentLinkedQueue<Ticket> queue = new ConcurrentLinkedQueue<>();
  6.  
  7. ExecutorService executor = Executors.newFixedThreadPool(10);
  8.  
  9. //create ticketProducer threads, each producing one ticket and adding it to queue
  10. for(int i=1; i<50; i++)
  11. {
  12. executor.execute(new ticketProducer(queue, i));
  13. }
  14. //create ticketConsumer threads, each consuming one ticket from queue.
  15. for(int j=1; j<50; j++)
  16. {
  17. executor.execute(new ticketConsumer(queue, j));
  18.  
  19. }
  20.  
  21.  
  22. /*
  23. for(int i=1; i<500; i++)
  24. {
  25. executor.execute(new ticketProducer(queue, i));
  26. executor.execute(new ticketConsumer(queue, i));
  27. }
  28. */
  29.  
  30.  
  31.  
  32. //stop executor when all threads are complete
  33. executor.shutdown();
  34.  
  35. }
  36.  
  37. }
  38.  
  39.  
  40. public class ticketProducer implements Runnable{
  41.  
  42. private ConcurrentLinkedQueue<Ticket> queue;
  43. private int i;
  44.  
  45. //Producer constructor, pass in reference to central q in the server main
  46. public ticketProducer(ConcurrentLinkedQueue<Ticket> queue2, int i){
  47. this.queue = queue2;
  48. this.i = i;
  49. }
  50.  
  51. @Override
  52. public void run(){
  53. //Create Ticket Objects, pass into q
  54.  
  55. //remove from loop, loop in main when calling executeTicketProducer, pass in i to this class in constructor?
  56. //not sure if this loop is ok or not
  57.  
  58. /*
  59. for(int i=1; i<50; i++)
  60. {
  61. Ticket newTicket = new Ticket(i);
  62. queue.add(newTicket);
  63. System.out.println("Created " + newTicket.getTicketID());
  64. }
  65. */
  66. Ticket newTicket = new Ticket(i);
  67. queue.add(newTicket);
  68. System.out.println("Created " + newTicket.getTicketID());
  69.  
  70.  
  71. }
  72.  
  73. }
  74.  
  75.  
  76. public class ticketConsumer implements Runnable {
  77.  
  78. private ConcurrentLinkedQueue<Ticket> queue;
  79. private int j;
  80.  
  81. //pass in queue
  82. public ticketConsumer(ConcurrentLinkedQueue<Ticket> queue2, int j){
  83. this.queue = queue2;
  84. this.j = j;
  85. }
  86.  
  87. @Override
  88. public void run(){
  89. //System.out.println("Consumer has started");
  90. Ticket consumedTicket;
  91. Ticket tempTicket;
  92.  
  93. //Thread.sleep(100);
  94. //consumedTicket = queue.poll();
  95. //System.out.println("Consumed " + consumedTicket.getTicketID());
  96.  
  97. /*
  98. while((tempTicket = queue.peek()) != null){
  99. consumedTicket = queue.poll();
  100. System.out.println("Consumed " + consumedTicket.getTicketID());
  101. }
  102. */
  103.  
  104. /*
  105. for(int x = 0; x < 50; x++);
  106. {
  107. while((tempTicket = queue.poll()) != null)
  108. {
  109. System.out.println("Consumed " + tempTicket.getTicketID());
  110. }
  111. try {
  112. Thread.currentThread().sleep(500);
  113. } catch (InterruptedException e) {
  114. // TODO Auto-generated catch block
  115. e.printStackTrace();
  116. }
  117. }
  118. */
  119.  
  120.  
  121. /*
  122. while((tempTicket = queue.peek()) != null)
  123. {
  124. //for(int x = 0; x<40; x++){
  125. queue.poll();
  126. System.out.println( j + "Consumed " + tempTicket.getTicketID());
  127. //}
  128. }
  129. */
  130. tempTicket = queue.poll();
  131. //int tempID = tempTicket.getTicketID();
  132. queue.remove();
  133. System.out.println( "Thread " + j + " consumed ticket " + tempTicket.getTicketID());
  134.  
  135.  
  136.  
  137. }
  138.  
  139. }
  140.  
  141.  
  142.  
  143. public class Ticket {
  144.  
  145. public static int id;
  146.  
  147. public Ticket(int ticketID)
  148. {
  149. id = ticketID;
  150. }
  151.  
  152. public static int getTicketID(){
  153. return id;
  154. }
  155.  
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment