Skip to content

Commit

Permalink
add ref project, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 14, 2023
1 parent 2d53f1a commit 1a8ce8d
Show file tree
Hide file tree
Showing 31 changed files with 824 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dev_projects/TddPlayGround/doc/progress.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
- 20231113
- p.74
- 20231114
- p.74
- p.84
5 changes: 5 additions & 0 deletions ref_project/java-concurrency-examples-master/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
.idea
out
*.iml
*.class
15 changes: 15 additions & 0 deletions ref_project/java-concurrency-examples-master/README.md
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.
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();
}
}
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();
}

}
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();
}

}
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();
}
}
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.
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;
}
}
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());
}
}
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());
}
}
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());
}
}
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.
Loading

0 comments on commit 1a8ce8d

Please sign in to comment.