-
Notifications
You must be signed in to change notification settings - Fork 0
/
qb155.java
37 lines (31 loc) · 947 Bytes
/
qb155.java
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
/*
Create PaymentThread class by extending Thread and Make a program to count payments
of 10 tickets each of 100 rs. Display total using main thread.
*/
class PaymentThread extends Thread {
static int ticket_price = 100;
static int total_payment = 0;
public void run() {
for (int i = 0; i < 10; i++) {
makePayment();
}
}
public synchronized void makePayment() {
total_payment += ticket_price;
System.out.println("Payment made for ticket: RS: " + ticket_price);
}
public static synchronized int getTotalPayment() {
return total_payment;
}
}
class QB155 {
public static void main(String[] args) {
PaymentThread pt = new PaymentThread();
pt.start();
try {
pt.join();
} catch (Exception e) {
}
System.out.println("Total payment for 10 Tickets: RS: " + PaymentThread.getTotalPayment());
}
}