Skip to content

Commit 5d6d610

Browse files
fork join framework example
1 parent a765564 commit 5d6d610

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package tr.salkan.code.java.threadExecutor.example.forkJoin.recursiveActionExample;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.ForkJoinPool;
6+
import java.util.concurrent.RecursiveAction;
7+
8+
public class ExampleTask extends RecursiveAction {
9+
10+
private static final long serialVersionUID = 1L;
11+
12+
private List<Integer> numberList;
13+
14+
public ExampleTask(List<Integer> numberList) {
15+
this.numberList = numberList;
16+
}
17+
18+
19+
@Override
20+
protected void compute() {
21+
22+
System.out.println("numberList :" + numberList);
23+
if(numberList != null && numberList.size() > 1) {
24+
List<Integer> oneNum = new ArrayList<Integer>();
25+
oneNum.add(numberList.remove(numberList.size()-1));
26+
27+
ExampleTask subTaskOne = new ExampleTask(oneNum);
28+
ExampleTask subTaskTwo = new ExampleTask(numberList);
29+
30+
invokeAll(subTaskOne, subTaskTwo);
31+
32+
}else {
33+
34+
computeFact(numberList.get(0));
35+
}
36+
37+
}
38+
39+
void computeFact(int num) {
40+
41+
System.out.println("computeFact :" + num);
42+
43+
int fact = 1;
44+
45+
for(int i = 1; i <=num; i++)
46+
{
47+
fact = fact * i;
48+
}
49+
50+
51+
System.out.println("Thread "+Thread.currentThread().getId()
52+
+" Number "+num
53+
+"factorial "+fact);
54+
}
55+
56+
public static void main(String[] args) {
57+
58+
ForkJoinPool fjp = new ForkJoinPool();
59+
60+
List<Integer> list = new ArrayList<>();
61+
list.add(5);
62+
list.add(8);
63+
list.add(11);
64+
list.add(3);
65+
66+
ExampleTask exampleTask = new ExampleTask(list);
67+
68+
fjp.invoke(exampleTask);
69+
70+
//or using
71+
72+
//fjp.execute(exampleTask);
73+
// exampleTask.join();
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package tr.salkan.code.java.threadExecutor.example.forkJoin.recursiveTaskExample;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.ForkJoinPool;
6+
import java.util.concurrent.RecursiveTask;
7+
8+
public class ExampleTask extends RecursiveTask<String> {
9+
10+
private static final long serialVersionUID = 1L;
11+
12+
private List<Double> salaries;
13+
14+
public ExampleTask(List<Double> salaries) {
15+
this.salaries = salaries;
16+
}
17+
18+
@Override
19+
protected String compute() {
20+
21+
if(salaries.size() == 1)
22+
{
23+
return computeTaxAndGetSalary(salaries.get(0));
24+
}
25+
26+
List<Double> oneSalary = salaries.subList(0, salaries.size()/2);
27+
List<Double> twoSalary = salaries.subList(salaries.size()/2,
28+
salaries.size());
29+
30+
ExampleTask taskOne = new ExampleTask(oneSalary);
31+
taskOne.fork();
32+
33+
ExampleTask taskTwo = new ExampleTask(twoSalary);
34+
35+
return taskTwo.compute()+"," + taskOne.join();
36+
37+
}
38+
39+
String computeTaxAndGetSalary(Double salary) {
40+
41+
Double taxRate = 1.0;
42+
43+
if(salary < 2000)
44+
{
45+
taxRate = 7.5;
46+
}
47+
else if( salary >=2000 || salary < 5000)
48+
{
49+
taxRate = 12.5;
50+
}
51+
else if ( salary >= 5000)
52+
{
53+
taxRate = 20.5;
54+
}
55+
56+
Double money = salary - ((salary*taxRate)/100);
57+
return "salary : " + salary + " taxRate :" + taxRate + " result : " + money;
58+
}
59+
60+
public static void main(String[] args) {
61+
62+
ForkJoinPool forkJoinPool = new ForkJoinPool();
63+
64+
List<Double> salaryList = new ArrayList<Double>();
65+
salaryList.add(3000.0);
66+
salaryList.add(2000.0);
67+
salaryList.add(1800.0);
68+
salaryList.add(9000.0);
69+
70+
ExampleTask exampleTask = new ExampleTask(salaryList);
71+
72+
String coupons = forkJoinPool.invoke(exampleTask);
73+
System.out.println(coupons);
74+
75+
}
76+
}

0 commit comments

Comments
 (0)