-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_starvation_solution
More file actions
36 lines (30 loc) · 1.04 KB
/
thread_starvation_solution
File metadata and controls
36 lines (30 loc) · 1.04 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
````
package thread;
class TicketBookingSystem implements Runnable {
int availableTickets = 1;
public void run() {
synchronized (this) {
if (this.availableTickets > 0) {
System.out.println("Ticket booking started by " + Thread.currentThread().getName());
try {
Thread.sleep(1000);
availableTickets--;
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Ticket booked by " + Thread.currentThread().getName());
System.out.println("Available tickets: " + this.availableTickets);
}
}
}
}
public class TheadStarvation {
public static void main(String[] args) {
TicketBookingSystem ticketBookingSystem = new TicketBookingSystem();
Thread t1 = new Thread(ticketBookingSystem, "Thread 1");
Thread t2 = new Thread(ticketBookingSystem, "Thread 2");
t1.start();
t2.start();
}
}
````