-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadExampleWithCounter.java
More file actions
37 lines (35 loc) · 1.3 KB
/
threadExampleWithCounter.java
File metadata and controls
37 lines (35 loc) · 1.3 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
/* Thread example with runnable inerface and lambda expression
* but the result is small count number because main thread ends before these threads can even end
* Can use join method so that they will execute and end before main thread and main thread goes in waiting state
* still it is not 2000 as we are doing for 2000 times(1000,1000) bcoz they may be executing together
* so to get 2000 we have to use synchornized modifier so only one thread works
*/
class Counter{
int count=0;
// can use synchornized in below to increase counter to 2000
public void Count(){
count++;
}
}
public class threadExampleWithCounter{
public static void main(String[] args) throws InterruptedException {
Counter c = new Counter();
Runnable A= () ->{
for (int i = 1; i <=1000; i++) {
c.Count();
}
};
Runnable B=()->{
for (int i = 1; i <=1000; i++) {
c.Count();
}
};
Thread t1=new Thread(A);
Thread t2=new Thread(B);
t1.start();
t2.start();
t1.join(); // join method can comment to get old output
t2.join(); // join method can comment to get old less output
System.out.println("Total counter after all threads: "+c.count);
}
}