-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexam25.java
37 lines (30 loc) · 1.12 KB
/
exam25.java
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
import java.util.Scanner;
public class exam25 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Reading input
System.out.println("Enter the number of numbers:");
int countOfNumbers = sc.nextInt();
System.out.println("Enter the number of threads:");
int numberOfThreads = sc.nextInt();
final int partitionSize = countOfNumbers / numberOfThreads;
int globalStart = 1;
while(globalStart < countOfNumbers){
final int start = globalStart;
final int end = start + partitionSize - 1;
for(int i=start;i<end;i+=partitionSize){
Thread t = new Thread(()-> {
System.out.println(Thread.currentThread().getName() + "counting numbers from "+start+" to "+end);
});
t.start();
try{
t.join();
}
catch (InterruptedException e){
e.getMessage();
}
}
globalStart = globalStart + partitionSize - 1;
}
}
}