-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompSim.java
109 lines (80 loc) · 2.54 KB
/
CompSim.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
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
package compsim;
import java.util.LinkedList;
import java.util.Queue;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author mohamedelkhawaga
*/
public class CompSim {
// Declare Queue
static Queue<Integer> q = new LinkedList<>();
static class GenrateTasks implements Runnable {
// Generate random tasks of max duration = sec in Milliseconds
int getRandMillsec(int sec) {
int x = (int) (Math.random() * 1000 * sec);
return x;
}
int getexpMillsec(double lambda){ // lambda is the avergae time between tasks in sec
double r1 = Math.random(); // URV from 0 to 1 millisec
double x = ((-1*(lambda*1000))*(Math.log(r1))) ;
return (int) x;
}
int taskMaxSec = 4;
double expAvgsec = 2.0;
@Override
public void run() {
while(true){
q.add(getRandMillsec(taskMaxSec));
try {
Thread.sleep(getexpMillsec(expAvgsec));
} catch (InterruptedException ex) {
Logger.getLogger(GenrateTasks.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(q.toString());
}
}
}
static class CompModel implements Runnable {
int task = 0;
public void setTask(int task) {
this.task = task;
}
// new THREAD(CLASSNAME).start();
@Override
public void run() {
while(true){
if( !(q.isEmpty())) {
System.out.print("IN");
task = q.poll();
try {
Thread.sleep(task);
} catch (InterruptedException ex) {
Logger.getLogger(CompModel.class.getName()).log(Level.SEVERE, null, ex);
}
}
else
System.out.print("EMPTY");
}
}
boolean compIsAvailble( CompModel c){
return this.task == 0;
}
}
public static void main(String[] args) throws InterruptedException {
// // Declare to computer objects
// CompModel comp1 = new CompModel();
// CompModel comp2 = new CompModel();
Runnable runnable1 = new GenrateTasks();
Runnable runnable2 = new CompModel();
Runnable runnable3 = new CompModel();
Thread thread1 = new Thread(runnable1);
Thread thread2= new Thread(runnable2);
Thread thread3 = new Thread(runnable3);
thread1.start();
thread2.start();
thread3.start();
//System.out.println(q.toString());
}
}