-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSockMachine.java
More file actions
139 lines (131 loc) · 3.67 KB
/
SockMachine.java
File metadata and controls
139 lines (131 loc) · 3.67 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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class SockMachine
{
//sock color counters
public static int red = 0;
public static int green = 0;
public static int blue = 0;
public static int orange = 0;
public static class Washer implements Callable //Class for Washing thread
{
public Washer()
{
}
public Object call() throws Exception
{
System.out.println("We are now in the Washer thread!");
red = 0;
green = 0;
blue = 0;
orange = 0;
System.out.println("The socks have been destroyed...");
return 0;
}
}
public static class Matcher implements Callable //Class for matching thread
{
public Matcher()
{
}
public Object call() throws Exception
{
//make number of sockes of each color even, so each is a pair
System.out.println("Inside the Matching Thread!");
if(red%2 != 0)
{
red--;
}
if(green%2 != 0)
{
green--;
}
if(blue%2 != 0)
{
blue--;
}
if(orange%2 != 0)
{
orange--;
}
System.out.println("We have " + red + " many pairs of red socks");
System.out.println("We have " + green + " many pairs of green socks");
System.out.println("We have " + blue + " many pairs of blue socks");
System.out.println("We have " + orange + " many pairs of orange socks");
return 1;
}
}
public static class Sock implements Callable
{
int color;
Random rand = new Random();
public Sock()
{
color = makeColor();
}
private int makeColor()
{
// 0 = red, 1 = green, 2 = blue, 3 = orange
int num;
num = rand.nextInt(4);
return num;
}
public int getColor()
{
return color;
}
@Override
public Object call() throws Exception
{
int numEachSocks = 0;
int amount = (rand.nextInt(100) + 1);
Sock[] socks = new Sock[amount];
System.out.println("Just entered " + Thread.currentThread().getName());
for(int i = 0; i < amount; i++)
{
socks[i] = new Sock();
if(socks[i].getColor() == 0)
{
red++;
}
else if(socks[i].getColor() == 1)
{
green++;
}
else if(socks[i].getColor() == 2)
{
blue++;
}
else if(socks[i].getColor() == 3)
{
orange++;
}
}
System.out.println(Thread.currentThread().getName() + " has just finished its run method and made " + amount + " socks");
return 0;
}
}
public static void main(String[] args) throws Exception
{
int length;
FutureTask matchTask = new FutureTask(new Matcher());
Thread matchingThread = new Thread(matchTask);
FutureTask washerTask = new FutureTask(new Washer());
Thread washerThread = new Thread(washerTask);
Sock[] arr = new Sock[100];
FutureTask[] tasks = new FutureTask[4];
Thread[] threads = new Thread[4];
for(int i = 0; i < tasks.length; i++)
{
tasks[i] = new FutureTask(new Sock());
threads[i] = new Thread(tasks[i]);
threads[i].start();
tasks[i].get();
}
matchingThread.sleep(1000);
matchingThread.start();
washerThread.sleep(1000);
washerThread.start();
}
}