Skip to content

Commit 63d3324

Browse files
committed
notes
1 parent 513ae83 commit 63d3324

File tree

472 files changed

+8646
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

472 files changed

+8646
-0
lines changed

.idea/.gitignore

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.ashfaq.dev.parallelcomputing;
2+
3+
class ParallelWorker extends Thread {
4+
5+
private int[] nums;
6+
private int low;
7+
private int high;
8+
private int partialSum;
9+
10+
public ParallelWorker(int[] nums, int low, int high) {
11+
this.nums = nums;
12+
this.low = low;
13+
this.high = Math.min(high, nums.length);
14+
}
15+
16+
public int getPartialSum() {
17+
return partialSum;
18+
}
19+
20+
@Override
21+
public void run() {
22+
23+
partialSum = 0;
24+
25+
for (int i = low; i < high; i++) {
26+
partialSum += nums[i];
27+
}
28+
}
29+
}
30+
31+
public class Sum_Problem_Parallel {
32+
33+
private ParallelWorker[] workers;
34+
35+
int numofthreads;
36+
37+
public Sum_Problem_Parallel(int numofthreads) {
38+
this.numofthreads = numofthreads;
39+
this.workers = new ParallelWorker[numofthreads]; // number of workers;
40+
}
41+
42+
public int getSum(int[] nums) {
43+
44+
int size = (int) Math.ceil(nums.length * 1.0 / numofthreads);
45+
46+
for (int i = 0; i < numofthreads; i++) {
47+
workers[i] = new ParallelWorker(nums, i * size, (i + 1) * size);
48+
// workers[i].start();
49+
workers[i].start();
50+
}
51+
52+
try {
53+
54+
for (ParallelWorker parallel_Worker : this.workers) {
55+
parallel_Worker.join();
56+
}
57+
58+
} catch (Exception e) {
59+
// TODO: handle exception
60+
}
61+
62+
int total = 0;
63+
64+
for (ParallelWorker parallel_Worker : this.workers) {
65+
total += parallel_Worker.getPartialSum();
66+
}
67+
68+
return total;
69+
70+
}
71+
72+
public static void main(String[] args) {
73+
74+
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
75+
Sum_Problem_Parallel sum_Problem_Parallel = new Sum_Problem_Parallel(3);
76+
System.out.println(sum_Problem_Parallel.getSum(nums));
77+
}
78+
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.ashfaq.dev.parallelcomputing;
2+
3+
public class Sum_Problem_Sequential {
4+
5+
public int sum(int[] nums) {
6+
int sum = 0;
7+
for (int i = 0; i < nums.length; i++) {
8+
9+
sum += nums[i];
10+
11+
}
12+
13+
return sum;
14+
}
15+
16+
public static void main(String[] args) {
17+
18+
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
19+
int sum = 0;
20+
Sum_Problem_Sequential sum_Problem_Sequential = new Sum_Problem_Sequential();
21+
sum = sum_Problem_Sequential.sum(nums);
22+
System.out.println(sum);
23+
}
24+
25+
}
6 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>ExecutorServices</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.balazsholczer.threads;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
6+
/**
7+
*
8+
* 1.) ExecutorService es = Executors.newCachedThreadPool();
9+
* - going to return an executorService that can dynamically
10+
* reuse threads
11+
* - before starting a job -> it going to check whether there are any threads that
12+
* finished the job...reuse them
13+
* - if there are no waiting threads -> it is going to create another one
14+
* - good for the processor ... effective solution !!!
15+
*
16+
* 2.) ExecutorService es = Executors.newFixedThreadPool(N);
17+
* - maximize the number of threads
18+
* - if we want to start a job -> if all the threads are busy, we have to wait for one
19+
* to terminate
20+
*
21+
* 3.) ExecutorService es = Executors.newSingleThreadExecutor();
22+
* It uses a single thread for the job
23+
*
24+
* execute() -> runnable + callable
25+
* submit() -> runnable
26+
27+
*/
28+
29+
class Worker implements Runnable{
30+
31+
@Override
32+
public void run() {
33+
for(int i=0;i<10;i++){
34+
System.out.println(i);
35+
try {
36+
Thread.sleep(300);
37+
} catch (InterruptedException e) {
38+
e.printStackTrace();
39+
}
40+
}
41+
}
42+
}
43+
44+
public class App {
45+
46+
public static void main(String[] args) {
47+
48+
ExecutorService executorService = Executors.newFixedThreadPool(5);
49+
50+
for(int i=0;i<5;i++){
51+
executorService.execute(new Worker());
52+
}
53+
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>FutureCallable</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.balazsholczer.threads;
2+
3+
import java.util.ArrayList;
4+
import java.util.Date;
5+
import java.util.List;
6+
import java.util.concurrent.Callable;
7+
import java.util.concurrent.ExecutorService;
8+
import java.util.concurrent.Executors;
9+
import java.util.concurrent.Future;
10+
11+
class Processor implements Callable<String>{
12+
13+
private int id;
14+
15+
public Processor(int id){
16+
this.id = id;
17+
}
18+
19+
@Override
20+
public String call() throws Exception {
21+
Thread.sleep(1000);
22+
return "Id: "+this.id;
23+
}
24+
}
25+
26+
public class App {
27+
28+
29+
public static void main(String[] args) {
30+
31+
ExecutorService executorService = Executors.newFixedThreadPool(2);
32+
List<Future<String>> list = new ArrayList<>();
33+
34+
for(int i=0;i<5;i++){
35+
Future<String> future = executorService.submit(new Processor(i+1));
36+
list.add(future);
37+
}
38+
39+
for(Future<String> future : list){
40+
try{
41+
System.out.println(future.get());
42+
}catch(Exception e){
43+
e.printStackTrace();
44+
}
45+
}
46+
47+
executorService.shutdown();
48+
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Concurrency1</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.balazsholczer.udemy;
2+
3+
class Runner1 {
4+
5+
public void startRunning() {
6+
for(int i=0;i<10;++i)
7+
System.out.println("Runner1: "+i);
8+
}
9+
}
10+
11+
class Runner2 {
12+
13+
public void startRunning() {
14+
for(int i=0;i<10;++i)
15+
System.out.println("Runner2: "+i);
16+
}
17+
}
18+
19+
public class App {
20+
21+
public static void main(String[] args) {
22+
23+
Runner1 runner1 = new Runner1();
24+
Runner2 runner2 = new Runner2();
25+
26+
runner1.startRunning();
27+
runner2.startRunning();
28+
29+
}
30+
}

0 commit comments

Comments
 (0)