-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTicketBookingTest.java
More file actions
38 lines (32 loc) · 1.1 KB
/
Copy pathTicketBookingTest.java
File metadata and controls
38 lines (32 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class TicketCounter {
private int ticketsAvailable = 10;
public synchronized void bookTicket() {
if (ticketsAvailable > 0) {
ticketsAvailable--;
System.out.println(" booked a ticket. Tickets left: " + ticketsAvailable);
} else {
System.out.println("No tickets available for ");
}
}
}
class TicketBookingTask implements Runnable {
private TicketCounter ticketCounter;
public TicketBookingTask(TicketCounter ticketCounter) {
this.ticketCounter = ticketCounter;
}
@Override
public void run() {
ticketCounter.bookTicket();
}
}
public class TicketBookingTest {
public static void main(String[] args) {
TicketCounter ticketCounter = new TicketCounter();
Thread thread1 = new Thread(new TicketBookingTask(ticketCounter));
Thread thread2 = new Thread(new TicketBookingTask(ticketCounter));
Thread thread3 = new Thread(new TicketBookingTask(ticketCounter));
thread1.start();
thread2.start();
thread3.start();
}
}