-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiThread.java
More file actions
39 lines (32 loc) · 1.06 KB
/
multiThread.java
File metadata and controls
39 lines (32 loc) · 1.06 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
39
import java.util.HashMap;
import java.util.Map;
class A {
int availableTickets=30;
Map<Integer,Integer> ticketMap=new HashMap<>();
public synchronized void bookTickets(int id,int tickets){
if(availableTickets >= tickets){
availableTickets -= tickets;
ticketMap.put(id, ticketMap.getOrDefault(id,0)+tickets);
System.out.println(ticketMap.get(id)+" -Tickets booked for :" +id);
}
else
System.out.println(availableTickets+" -Tickets are only available");
}
}
public class multiThread{
public static void main(String args[]){
A a1=new A();
Runnable book = ()->a1.bookTickets(1, 9);
Runnable book1 = ()->a1.bookTickets(2, 6);
Runnable book2 = ()->a1.bookTickets(3, 17);
Runnable book3 = ()->a1.bookTickets(1, 10);
Thread t1=new Thread(book);
Thread t2=new Thread(book1);
Thread t3=new Thread(book2);
Thread t4=new Thread(book3);
t1.start();
t2.start();
t3.start();
t4.start();
}
}