-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_starvation
More file actions
34 lines (28 loc) · 878 Bytes
/
thread_starvation
File metadata and controls
34 lines (28 loc) · 878 Bytes
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
````
package thread;
class TicketBookingSystem implements Runnable {
int availableTickets = 1;
public void run() {
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();
}
}
````