-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9_Multithreading.java
More file actions
62 lines (52 loc) · 1.25 KB
/
9_Multithreading.java
File metadata and controls
62 lines (52 loc) · 1.25 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
import java.util.Date;
public class Multithreading implements Runnable
{
int startCount;
static long total;
String name;
Thread t;
public Multithreading(String name, int startCount)
{
this.name = name;
this.startCount = startCount;
t = new Thread(this, name);
t.start();
}
public void run()
{
System.out.println(Thread.currentThread().getName() + " running ...");
long sum = 0;
for(int i = startCount; i < startCount +10; i++)
sum += i;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Sum of " + startCount + " to " + (startCount+9) + " = " + sum);
total += sum;
}
public static void main(String[] args)
{
int startCount = 1;
Multithreading ob[] = new Multithreading[5];
Date start = new Date();
for(int i = 0; i < 5; i++)
{
String tName = "Thread_" + i;
ob[i] = new Multithreading(tName, startCount);
startCount += 10;
}
try{
for(int i = 0; i < 5; i++)
ob[i].t.join();
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Total = " + total);
Date end = new Date();
long diff = end.getTime() - start.getTime();
System.out.println("Computation time : " + diff + "ms");
}
}