forked from isenmit/javalab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiThreading.java
More file actions
161 lines (146 loc) · 3.79 KB
/
MultiThreading.java
File metadata and controls
161 lines (146 loc) · 3.79 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import java.util.Date;
public class MultiThreading implements Runnable
{
Thread t;
static int[] a=new int[51];
static int sum=0;
MultiThreading(String name)
{
t=new Thread(this, name);
System.out.println("childthread:"+t);
t.start();
}
public void run()
{
System.out.println(Thread.currentThread().getName());
if(Thread.currentThread().getName().compareTo("one")==0)
{
for(int i=0;i<10;i++)
{
sum=sum+a[i];
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("Sum of 1-10 : " +sum);
}
//total=total+sum;
}
else if(Thread.currentThread().getName().compareTo("two")==0)
{
for(int j=10;j<20;j++)
{
sum=sum+a[j];
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Sum of 10-20 : " +sum);
}
//total=total+sum;
}
else if(Thread.currentThread().getName().compareTo("three")==0)
{
for(int k=20;k<30;k++)
{
sum=sum+a[k];
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("Sum of 20-30 : " +sum);
}
//total=total+sum;
}
else if(Thread.currentThread().getName().compareTo("four")==0)
{
for(int l=30;l<40;l++)
{
sum=sum+a[l];
try{
Thread.sleep(1000);
}catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Sum of 30-40 : " +sum);
}
//total=total+sum;
}
else if(Thread.currentThread().getName().compareTo("five")==0)
{
for(int m=40;m<50;m++)
{
sum=sum+a[m];
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("Sum of 40-50 : " +sum);
//total=total+sum;
}
//System.out.println("Total sum is : " +total);
}
}
public static void main(String[] args)
{
for(int x=0;x<51;x++)
{
a[x]=x+1;
}
System.out.println(Thread.currentThread().getName());
MultiThreading ob1=new MultiThreading("one");
MultiThreading ob2=new MultiThreading("two");
MultiThreading ob3=new MultiThreading("three");
MultiThreading ob4=new MultiThreading("four");
MultiThreading ob5=new MultiThreading("five");
Date start=new Date();
System.out.println("First Thread is Alive? : " +ob1.t.isAlive());
System.out.println("Second Thread is Alive? : " +ob2.t.isAlive());
System.out.println("Third Thread is Alive? : " +ob3.t.isAlive());
System.out.println("Fourth Thread is Alive? : " +ob4.t.isAlive());
System.out.println("Fivth Thread is Alive? : " +ob5.t.isAlive());
try
{
System.out.println("waiting for Threads to complete");
ob1.t.join();
ob2.t.join();
ob3.t.join();
ob4.t.join();
ob5.t.join();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Total sum is : " +sum);
System.out.println("First Thread is Alive? :"+ob1.t.isAlive());
System.out.println("Second Thread is Alive? :"+ob2.t.isAlive());
System.out.println("Third Thread is Alive? :"+ob3.t.isAlive());
System.out.println("Fourth Thread is Alive? :"+ob4.t.isAlive());
System.out.println("Fivth Thread is Alive? :"+ob5.t.isAlive());
System.out.println("Main thread is interupted ");
Date end=new Date();
long difference=end.getTime()-start.getTime();
System.out.println("Whole process took "+difference/1000 +" " +"seconds");
System.out.println("Mian thread is exiting");
}
}