-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathPhonePurchase.java
72 lines (60 loc) · 1.74 KB
/
PhonePurchase.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.util.*;
class Sell {
static Vector phones;
public Sell(Vector phones) {
this.phones = phones;
}
static synchronized void addPhone(String s) {
if (phones.size() < 20) {
phones.add(s);
}
}
}
class Stores extends Thread {
Random rand = new Random(); // generating a random number
int max = 750; // max wait time for thread
int min = 100; // min wait time for thread
int j;
Vector phones;
Sell s;
public Stores(Vector phones) {
this.phones = phones;
s = new Sell(phones);
}
public void run() {
try {
while (phones.size() < 20) {
j = rand.nextInt((max - min) + 1) + min;
System.out.println(this.getName() + " is sleeping for " + j);
Thread.sleep(j); // create random delay between threads
System.out.println(Thread.currentThread().getName() + " sold a phone");
s.addPhone(Thread.currentThread().getName());
}
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
public class PhonePurchase {
public static void main(String[] args) {
Vector phonesSold = new Vector(20);
Stores A = new Stores(phonesSold);
A.setName("A");
Stores F = new Stores(phonesSold);
F.setName("F");
Stores E = new Stores(phonesSold);
E.setName("E");
A.start();
F.start();
E.start();
try {
A.join();
F.join();
E.join();
} catch (InterruptedException e) {
System.out.println(e);
}
System.out.println(phonesSold);
System.out.println(phonesSold.size());
}
}