-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThreadCS.cs
More file actions
59 lines (51 loc) · 1.7 KB
/
Copy pathThreadCS.cs
File metadata and controls
59 lines (51 loc) · 1.7 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
using System;
using System.Threading;
namespace ThreadCS {
class ThreadCS {
static void Main(string[] args)
{
int num = 0;
try {
num = Int32.Parse(args[1]);
} catch (Exception e) {
Console.WriteLine("Invalid argument: {0}", e.Message);
return;
}
Runnable runnable = new Runnable(num);
Thread t0 = new Thread(new ThreadStart(runnable.run));
Thread t1 = new Thread(new ThreadStart(runnable.run));
Thread t2 = new Thread(new ThreadStart(runnable.run));
Thread t3 = new Thread(new ThreadStart(runnable.run));
Console.WriteLine("C# Threads - initializing complete");
t0.Start();
t1.Start();
t2.Start();
t3.Start();
try {
t0.Join();
t1.Join();
t2.Join();
t3.Join();
} catch (Exception e) {
Console.WriteLine("Join failed: {0}", e.Message);
}
}
}
class Runnable {
private int num;
private long startAt;
public Runnable(int num) {
this.num = num;
this.startAt = DateTimeOffset.Now.ToUnixTimeMilliseconds();
}
public void run() {
Prime prime = new Prime(this.num);
int count = prime.count();
long endAt = DateTimeOffset.Now.ToUnixTimeMilliseconds() - this.startAt;
Console.WriteLine("C# thread id #{0}: executes {1} prime numbers in {2} ms",
Thread.CurrentThread.ManagedThreadId,
count,
endAt);
}
}
}