-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ecd8fb0
commit 2d8922b
Showing
8 changed files
with
313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>study-concurrency-api</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
11 changes: 11 additions & 0 deletions
11
study-concurrency-api/.settings/org.eclipse.jdt.core.prefs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.8 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.8 |
51 changes: 51 additions & 0 deletions
51
study-concurrency-api/src/com/example/StudyCallableThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.example; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.FutureTask; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* @author Binnur Kurt <binnur.kurt@gmail.com> | ||
*/ | ||
public class StudyCallableThread { | ||
|
||
public static void main(String[] args) throws InterruptedException, ExecutionException { | ||
LotteryCallableTask task = new LotteryCallableTask(1, 50, 6); | ||
FutureTask<List<Integer>> future = | ||
new FutureTask<>(task); | ||
Thread t = new Thread(future); | ||
t.start(); | ||
while(true) { | ||
try { | ||
System.err.println(future.get(10, | ||
TimeUnit.MILLISECONDS)); | ||
break; | ||
} catch (TimeoutException e) { | ||
System.err.println("I am doing another task..."); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
class LotteryCallableTask implements Callable<List<Integer>> { | ||
private int min, max, size; | ||
|
||
public LotteryCallableTask(int min, int max, int size) { | ||
this.min = min; | ||
this.max = max; | ||
this.size = size; | ||
} | ||
|
||
@Override | ||
public List<Integer> call() { | ||
return ThreadLocalRandom.current().ints(min, max).distinct().limit(size).sorted().boxed() | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
study-concurrency-api/src/com/example/StudyDataParallelism.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.example; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
import java.util.stream.IntStream; | ||
|
||
/** | ||
* @author Binnur Kurt <binnur.kurt@gmail.com> | ||
*/ | ||
public class StudyDataParallelism { | ||
private static List<Integer> numbers = new ArrayList<>(); | ||
|
||
static { | ||
IntStream.range(0, 400_000).forEach(numbers::add); | ||
} | ||
|
||
public static void main(String[] args) { | ||
for (int i = 0; i < 10; ++i) | ||
runSerialSolver(); | ||
for (int i = 0; i < 10; ++i) | ||
runParallelSolver(); | ||
for (int i = 0; i < 10; ++i) | ||
runParallelStreamSolver(); | ||
|
||
} | ||
|
||
private static void runParallelStreamSolver() { | ||
long start = System.nanoTime(); | ||
long sum = numbers.parallelStream().mapToLong(Integer::intValue) | ||
.sum(); | ||
long stop = System.nanoTime(); | ||
System.err.println(String.format("Parallel Stream: %16d %8d", sum, (stop - start))); | ||
} | ||
|
||
private static void runParallelSolver() { | ||
int cpus = Runtime.getRuntime().availableProcessors(); | ||
ExecutorService es = Executors.newFixedThreadPool(cpus); | ||
long start = System.nanoTime(); | ||
List<Future<Long>> futures = new ArrayList<>(); | ||
int size = numbers.size() / cpus; | ||
for (int i = 0, k = 0; i < cpus; ++i, k += size) { | ||
futures.add(es.submit(new ParallelSolverTask(k, size, numbers))); | ||
} | ||
long sum = 0; | ||
for (Future<Long> partialSum : futures) | ||
try { | ||
sum += partialSum.get(); | ||
} catch (InterruptedException | ExecutionException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
|
||
long stop = System.nanoTime(); | ||
System.err.println(String.format("Parallel: %16d %8d", sum, (stop - start))); | ||
es.shutdown(); | ||
} | ||
|
||
private static void runSerialSolver() { | ||
long start = System.nanoTime(); | ||
long sum = 0L; | ||
for (int num : numbers) | ||
sum += num; | ||
long stop = System.nanoTime(); | ||
System.err.println(String.format("Serial: %16d %8d", sum, (stop - start))); | ||
} | ||
|
||
} | ||
|
||
class ParallelSolverTask implements Callable<Long> { | ||
private int start; | ||
private int size; | ||
private List<Integer> numbers; | ||
|
||
public ParallelSolverTask(int start, int size, List<Integer> numbers) { | ||
this.start = start; | ||
this.size = size; | ||
this.numbers = numbers; | ||
} | ||
|
||
@Override | ||
public Long call() throws Exception { | ||
long sum = 0L; | ||
for (int i = start, j = 0; j < size; i++, j++) | ||
sum += numbers.get(i); | ||
return sum; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.example; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
|
||
/** | ||
* @author Binnur Kurt <binnur.kurt@gmail.com> | ||
*/ | ||
public class StudyExecutors { | ||
|
||
public static void main(String[] args) throws InterruptedException, ExecutionException { | ||
ExecutorService es = Executors.newFixedThreadPool(10); | ||
LotteryCallableTask task = | ||
new LotteryCallableTask(1, 50, 6); | ||
Future<List<Integer>> future = | ||
es.submit(task); | ||
System.out.println(future.get()); | ||
es.shutdown(); | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
study-concurrency-api/src/com/example/StudyRaceCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.example; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.stream.IntStream; | ||
|
||
/** | ||
* @author Binnur Kurt <binnur.kurt@gmail.com> | ||
*/ | ||
public class StudyRaceCondition { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
CounterTask task = new CounterTask(); | ||
List<Thread> threads = new ArrayList<>(); | ||
IntStream.range(0, 8).forEach(i -> threads.add(new Thread(task))); | ||
long start = System.currentTimeMillis(); | ||
threads.forEach(Thread::start); | ||
threads.forEach(t -> { | ||
try { | ||
t.join(); | ||
} catch (InterruptedException e) { | ||
} | ||
}); | ||
long stop = System.currentTimeMillis(); | ||
System.out.println(task.getCounter() + " @ " + (stop - start) + " ms."); | ||
} | ||
|
||
} | ||
|
||
// Thread Safe | ||
class CounterTask implements Runnable { | ||
private int counter; | ||
|
||
public int getCounter() { | ||
return counter; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
for (int i = 0; i < 100_000; ++i) { | ||
synchronized (this) { | ||
counter++; // critical section, mutual exclusion | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
class LockFreeCounterTask implements Runnable { | ||
private AtomicInteger counter = new AtomicInteger(0); | ||
|
||
public int getCounter() { | ||
return counter.get(); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
for (int i = 0; i < 100_000; ++i) { | ||
counter.getAndIncrement(); | ||
} | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
study-concurrency-api/src/com/example/StudyRunnableThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.example; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* @author Binnur Kurt <binnur.kurt@gmail.com> | ||
*/ | ||
public class StudyRunnableThread { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
LotteryTask task = new LotteryTask(1, 50, 6); | ||
Thread t = new Thread(task); | ||
t.start(); | ||
t.join(); | ||
System.out.println(task.getNumbers()); | ||
} | ||
|
||
} | ||
// byte --> Byte (Wrapper Class) (Immutable) | ||
// short --> Short | ||
// int --> Integer | ||
// long --> Long | ||
// boolean --> Boolean | ||
// char --> Character | ||
|
||
class LotteryTask implements Runnable { | ||
private int min, max, size; | ||
private List<Integer> numbers; | ||
|
||
public LotteryTask(int min, int max, int size) { | ||
this.min = min; | ||
this.max = max; | ||
this.size = size; | ||
} | ||
|
||
public List<Integer> getNumbers() { | ||
return numbers; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
numbers = ThreadLocalRandom.current().ints(min, max).distinct().limit(size).sorted().boxed() | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
} |