Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ticketServer {
- public static void main(String[] args) {
- ConcurrentLinkedQueue<Ticket> queue = new ConcurrentLinkedQueue<>();
- ExecutorService executor = Executors.newFixedThreadPool(10);
- //create ticketProducer threads, each producing one ticket and adding it to queue
- for(int i=1; i<50; i++)
- {
- executor.execute(new ticketProducer(queue, i));
- }
- //create ticketConsumer threads, each consuming one ticket from queue.
- for(int j=1; j<50; j++)
- {
- executor.execute(new ticketConsumer(queue, j));
- }
- /*
- for(int i=1; i<500; i++)
- {
- executor.execute(new ticketProducer(queue, i));
- executor.execute(new ticketConsumer(queue, i));
- }
- */
- //stop executor when all threads are complete
- executor.shutdown();
- }
- }
- public class ticketProducer implements Runnable{
- private ConcurrentLinkedQueue<Ticket> queue;
- private int i;
- //Producer constructor, pass in reference to central q in the server main
- public ticketProducer(ConcurrentLinkedQueue<Ticket> queue2, int i){
- this.queue = queue2;
- this.i = i;
- }
- @Override
- public void run(){
- //Create Ticket Objects, pass into q
- //remove from loop, loop in main when calling executeTicketProducer, pass in i to this class in constructor?
- //not sure if this loop is ok or not
- /*
- for(int i=1; i<50; i++)
- {
- Ticket newTicket = new Ticket(i);
- queue.add(newTicket);
- System.out.println("Created " + newTicket.getTicketID());
- }
- */
- Ticket newTicket = new Ticket(i);
- queue.add(newTicket);
- System.out.println("Created " + newTicket.getTicketID());
- }
- }
- public class ticketConsumer implements Runnable {
- private ConcurrentLinkedQueue<Ticket> queue;
- private int j;
- //pass in queue
- public ticketConsumer(ConcurrentLinkedQueue<Ticket> queue2, int j){
- this.queue = queue2;
- this.j = j;
- }
- @Override
- public void run(){
- //System.out.println("Consumer has started");
- Ticket consumedTicket;
- Ticket tempTicket;
- //Thread.sleep(100);
- //consumedTicket = queue.poll();
- //System.out.println("Consumed " + consumedTicket.getTicketID());
- /*
- while((tempTicket = queue.peek()) != null){
- consumedTicket = queue.poll();
- System.out.println("Consumed " + consumedTicket.getTicketID());
- }
- */
- /*
- for(int x = 0; x < 50; x++);
- {
- while((tempTicket = queue.poll()) != null)
- {
- System.out.println("Consumed " + tempTicket.getTicketID());
- }
- try {
- Thread.currentThread().sleep(500);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- */
- /*
- while((tempTicket = queue.peek()) != null)
- {
- //for(int x = 0; x<40; x++){
- queue.poll();
- System.out.println( j + "Consumed " + tempTicket.getTicketID());
- //}
- }
- */
- tempTicket = queue.poll();
- //int tempID = tempTicket.getTicketID();
- queue.remove();
- System.out.println( "Thread " + j + " consumed ticket " + tempTicket.getTicketID());
- }
- }
- public class Ticket {
- public static int id;
- public Ticket(int ticketID)
- {
- id = ticketID;
- }
- public static int getTicketID(){
- return id;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment