-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
31 changed files
with
824 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -6,4 +6,4 @@ | |
- 20231113 | ||
- p.74 | ||
- 20231114 | ||
- p.74 | ||
- p.84 |
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,5 @@ | ||
.DS_Store | ||
.idea | ||
out | ||
*.iml | ||
*.class |
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,15 @@ | ||
# Java Concurrency/Multithreading Examples | ||
|
||
## Relevant Tutorials | ||
|
||
1. [Java Concurrency / Multithreading Basics](https://www.callicoder.com/java-concurrency-multithreading-basics/) | ||
|
||
2. [Java Thread and Runnable Tutorial](https://www.callicoder.com/java-multithreading-thread-and-runnable-tutorial/) | ||
|
||
3. [Java Executors and Thread Pool Tutorial](https://www.callicoder.com/java-executor-service-and-thread-pool-tutorial/) | ||
|
||
4. [Java Callable and Future Tutorial](https://www.callicoder.com/java-callable-and-future-tutorial/) | ||
|
||
5. [Java Concurrency issues and Synchronization](https://www.callicoder.com/java-concurrency-issues-and-thread-synchronization/) | ||
|
||
6. [Java Locks and Atomic Variables](https://www.callicoder.com/java-locks-and-atomic-variables-tutorial/) |
Empty file.
29 changes: 29 additions & 0 deletions
29
...rency-examples-master/java-callable-and-future-examples/src/FutureAndCallableExample.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,29 @@ | ||
import java.util.concurrent.*; | ||
|
||
/** | ||
* Created by rajeevkumarsingh on 11/05/17. | ||
*/ | ||
public class FutureAndCallableExample { | ||
public static void main(String[] args) throws InterruptedException, ExecutionException { | ||
ExecutorService executorService = Executors.newSingleThreadExecutor(); | ||
|
||
Callable<String> callable = () -> { | ||
// Perform some computation | ||
System.out.println("Entered Callable"); | ||
Thread.sleep(2000); | ||
return "Hello from Callable"; | ||
}; | ||
|
||
System.out.println("Submitting Callable"); | ||
Future<String> future = executorService.submit(callable); | ||
// This line executes immediately | ||
System.out.println("Do something else while callable is getting executed"); | ||
|
||
System.out.println("Retrieve the result of the future"); | ||
// Future.get() blocks until the result is available | ||
String result = future.get(); | ||
System.out.println(result); | ||
|
||
executorService.shutdown(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...oncurrency-examples-master/java-callable-and-future-examples/src/FutureCancelExample.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,43 @@ | ||
import java.util.concurrent.*; | ||
|
||
/** | ||
* Created by rajeevkumarsingh on 10/05/17. | ||
*/ | ||
public class FutureCancelExample { | ||
public static void main(String[] args) throws InterruptedException, ExecutionException { | ||
ExecutorService executorService = Executors.newSingleThreadExecutor(); | ||
|
||
Callable<String> callable = () -> { | ||
// Perform some computation | ||
Thread.sleep(2000); | ||
return "Hello from Callable"; | ||
}; | ||
|
||
long startTime = System.nanoTime(); | ||
Future<String> future = executorService.submit(callable); | ||
|
||
while(!future.isDone()) { | ||
System.out.println("Task is still not done..."); | ||
Thread.sleep(200); | ||
double elapsedTimeInSec = (System.nanoTime() - startTime) / 1000000000.0; | ||
|
||
if (elapsedTimeInSec > 1) { | ||
// cancel future if the elapsed time is more than one second | ||
future.cancel(true); | ||
} | ||
} | ||
|
||
// Check if future is cancelled before retrieving the result | ||
if(!future.isCancelled()) { | ||
System.out.println("Task completed! Retrieving the result"); | ||
// Future.get() blocks until the result is available | ||
String result = future.get(); | ||
System.out.println(result); | ||
} else { | ||
System.out.println("Task was cancelled"); | ||
} | ||
|
||
executorService.shutdown(); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
...oncurrency-examples-master/java-callable-and-future-examples/src/FutureIsDoneExample.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,27 @@ | ||
import java.util.concurrent.*; | ||
|
||
/** | ||
* Created by rajeevkumarsingh on 11/05/17. | ||
*/ | ||
public class FutureIsDoneExample { | ||
public static void main(String[] args) throws InterruptedException, ExecutionException { | ||
ExecutorService executorService = Executors.newSingleThreadExecutor(); | ||
|
||
Future<String> future = executorService.submit(() -> { | ||
Thread.sleep(2000); | ||
return "Hello from Callable"; | ||
}); | ||
|
||
while(!future.isDone()) { | ||
System.out.println("Task is still not done..."); | ||
Thread.sleep(200); | ||
} | ||
|
||
System.out.println("Task completed! Retrieving the result"); | ||
String result = future.get(); | ||
System.out.println(result); | ||
|
||
executorService.shutdown(); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...a-concurrency-examples-master/java-callable-and-future-examples/src/InvokeAllExample.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,38 @@ | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.concurrent.*; | ||
|
||
/** | ||
* Created by rajeevkumarsingh on 10/05/17. | ||
*/ | ||
public class InvokeAllExample { | ||
public static void main(String[] args) throws InterruptedException, ExecutionException { | ||
ExecutorService executorService = Executors.newFixedThreadPool(5); | ||
|
||
Callable<String> task1 = () -> { | ||
Thread.sleep(2000); | ||
return "Result of Task1"; | ||
}; | ||
|
||
Callable<String> task2 = () -> { | ||
Thread.sleep(1000); | ||
return "Result of Task2"; | ||
}; | ||
|
||
Callable<String> task3 = () -> { | ||
Thread.sleep(5000); | ||
return "Result of Task3"; | ||
}; | ||
|
||
List<Callable<String>> taskList = Arrays.asList(task1, task2, task3); | ||
|
||
List<Future<String>> futures = executorService.invokeAll(taskList); | ||
|
||
for(Future<String> future: futures) { | ||
// The result is printed only after all the futures are complete. (i.e. after 5 seconds) | ||
System.out.println(future.get()); | ||
} | ||
|
||
executorService.shutdown(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...a-concurrency-examples-master/java-callable-and-future-examples/src/InvokeAnyExample.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,34 @@ | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.concurrent.*; | ||
|
||
/** | ||
* Created by rajeevkumarsingh on 10/05/17. | ||
*/ | ||
public class InvokeAnyExample { | ||
public static void main(String[] args) throws InterruptedException, ExecutionException { | ||
ExecutorService executorService = Executors.newFixedThreadPool(5); | ||
|
||
Callable<String> task1 = () -> { | ||
Thread.sleep(2000); | ||
return "Result of Task1"; | ||
}; | ||
|
||
Callable<String> task2 = () -> { | ||
Thread.sleep(1000); | ||
return "Result of Task2"; | ||
}; | ||
|
||
Callable<String> task3 = () -> { | ||
Thread.sleep(5000); | ||
return "Result of Task3"; | ||
}; | ||
|
||
// Returns the result of the fastest callable. (task2 in this case) | ||
String result = executorService.invokeAny(Arrays.asList(task1, task2, task3)); | ||
|
||
System.out.println(result); | ||
|
||
executorService.shutdown(); | ||
} | ||
} |
Empty file.
30 changes: 30 additions & 0 deletions
30
...master/java-concurrency-issues-and-synchronization/src/MemoryConsistencyErrorExample.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,30 @@ | ||
public class MemoryConsistencyErrorExample { | ||
private static boolean sayHello = false; | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
|
||
Thread thread = new Thread(() -> { | ||
while(!sayHello) { | ||
|
||
} | ||
|
||
System.out.println("Hello World!"); | ||
|
||
while(sayHello) { | ||
|
||
} | ||
|
||
System.out.println("Good Bye!"); | ||
}); | ||
|
||
thread.start(); | ||
|
||
Thread.sleep(1000); | ||
System.out.println("Say Hello.."); | ||
sayHello = true; | ||
|
||
Thread.sleep(1000); | ||
System.out.println("Say Bye.."); | ||
sayHello = false; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...examples-master/java-concurrency-issues-and-synchronization/src/RaceConditionExample.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,36 @@ | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Created by rajeevkumarsingh on 11/05/17. | ||
*/ | ||
class Counter { | ||
private int count = 0; | ||
|
||
public void increment() { | ||
count = count + 1; | ||
} | ||
|
||
public int getCount() { | ||
return count; | ||
} | ||
} | ||
|
||
public class RaceConditionExample { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
ExecutorService executorService = Executors.newFixedThreadPool(10); | ||
|
||
Counter counter = new Counter(); | ||
|
||
for(int i = 0; i < 1000; i++) { | ||
executorService.submit(() -> counter.increment()); | ||
} | ||
|
||
executorService.shutdown(); | ||
executorService.awaitTermination(60, TimeUnit.SECONDS); | ||
|
||
System.out.println("Final count is : " + counter.getCount()); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...ples-master/java-concurrency-issues-and-synchronization/src/SynchronizedBlockExample.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,37 @@ | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Created by rajeevkumarsingh on 11/05/17. | ||
*/ | ||
class FineGrainedSynchronizedCounter { | ||
private int count = 0; | ||
|
||
public void increment() { | ||
// Synchronized Block | ||
synchronized (this) { | ||
count = count + 1; | ||
} | ||
} | ||
|
||
public int getCount() { | ||
return count; | ||
} | ||
} | ||
|
||
public class SynchronizedBlockExample { | ||
public static void main(String[] args) throws InterruptedException { | ||
ExecutorService executorService = Executors.newFixedThreadPool(10); | ||
FineGrainedSynchronizedCounter counter = new FineGrainedSynchronizedCounter(); | ||
|
||
for(int i = 0; i < 1000; i++) { | ||
executorService.submit(() -> counter.increment()); | ||
} | ||
|
||
executorService.shutdown(); | ||
executorService.awaitTermination(60, TimeUnit.SECONDS); | ||
|
||
System.out.println("Final count is " + counter.getCount()); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...les-master/java-concurrency-issues-and-synchronization/src/SynchronizedMethodExample.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,37 @@ | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Created by rajeevkumarsingh on 11/05/17. | ||
*/ | ||
class SynchronizedCounter { | ||
private int count = 0; | ||
|
||
// Synchronized Method | ||
public synchronized void increment() { | ||
System.out.println(Thread.currentThread().getName()); | ||
count = count + 1; | ||
} | ||
|
||
public int getCount() { | ||
return count; | ||
} | ||
} | ||
|
||
public class SynchronizedMethodExample { | ||
public static void main(String[] args) throws InterruptedException { | ||
ExecutorService executorService = Executors.newFixedThreadPool(10); | ||
|
||
SynchronizedCounter synchronizedCounter = new SynchronizedCounter(); | ||
|
||
for(int i = 0; i < 1000; i++) { | ||
executorService.submit(() -> synchronizedCounter.increment()); | ||
} | ||
|
||
executorService.shutdown(); | ||
executorService.awaitTermination(60, TimeUnit.SECONDS); | ||
|
||
System.out.println("Final count is : " + synchronizedCounter.getCount()); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...amples-master/java-concurrency-issues-and-synchronization/src/VolatileKeywordExample.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,30 @@ | ||
public class VolatileKeywordExample { | ||
private static volatile boolean sayHello = false; | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
|
||
Thread thread = new Thread(() -> { | ||
while(!sayHello) { | ||
|
||
} | ||
|
||
System.out.println("Hello World!"); | ||
|
||
while(sayHello) { | ||
|
||
} | ||
|
||
System.out.println("Good Bye!"); | ||
}); | ||
|
||
thread.start(); | ||
|
||
Thread.sleep(1000); | ||
System.out.println("Say Hello.."); | ||
sayHello = true; | ||
|
||
Thread.sleep(1000); | ||
System.out.println("Say Bye.."); | ||
sayHello = false; | ||
} | ||
} |
Empty file.
Oops, something went wrong.