-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpmt1.scala
97 lines (88 loc) · 2.29 KB
/
mpmt1.scala
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
/**
* mpmt1.scala: A Scala version of mpmt1
*
* STATUS:
* Under development
* License:
* Apache License, Version 2.0
* History:
* 2022/01/01 v0.1 Initial version.
* Author:
* Masanori Itoh <masanori.itoh@gmail.com>
* TODO:
* * Write multi process version?
*/
//break requires scala 2.8 or later.
import scala.util.control.Breaks._
object mpmt1 {
def main(args: Array[String]) = {
var num_context = 4
var duration = 30
var mode = "t"
//Parse options.
//NOTE(thatsdone): Avoided to use getopt pacakge for now.
for (i <- 0 until args.length) {
if (args(i) == "-n") {
if (args(i + 1).startsWith("-")) {
println("-n requires a value")
sys.exit()
}
num_context = args(i + 1).toInt
} else if (args(i) == "-d") {
if (args(i + 1).startsWith("-")) {
println("-d requires a value")
sys.exit()
}
duration = args(i + 1).toInt
} else if (args(i) == "-m") {
//ignore at the moment
if (args(i + 1).startsWith("-")) {
println("-m requires a value")
sys.exit()
}
//mode = args(i + 1)
println("Currently -m option is ignored.")
} else {
//ignore at the moment
}
}
printf("num_context: %d duration %d (s) mode: %s\n",
num_context, duration, mode)
val threads = {
for (i <- 0 until num_context)
yield new Thread(new BusyWorker(num_context, duration))
}
threads.foreach({
t => t.start()
println("Thread started: " + t)
})
threads.foreach({
t => t.join()
println("Thread completed: " + t)
})
}
}
class BusyWorker(num_context: Int, duration: Int) extends Runnable {
def run() = {
var ts_save = System.currentTimeMillis()
var ts = ts_save
//duration is in seconds. max is in milli-seconds
val max = duration * 1000
//Don't use Scala3 syntax for now.
/**
while ({
ts = System.currentTimeMillis()
(ts - ts_save) <= max
}) ()
// The above () or {} is necessary.
*/
//Use scala.util.controk.Breaks for 'break'
breakable {
while (true) {
ts = System.currentTimeMillis()
if ((ts - ts_save) > max) break;
}
}
println("Expired! " + (ts - ts_save))
}
}