forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
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
62 changed files
with
1,689 additions
and
36 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
26 changes: 26 additions & 0 deletions
26
...va-concurrency/src/main/java/com/baeldung/concurrent/executorservice/DelayedCallable.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,26 @@ | ||
package com.baeldung.concurrent.executorservice; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
public class DelayedCallable implements Callable<String> { | ||
|
||
private String name; | ||
private long period; | ||
|
||
public DelayedCallable(String name, long period) { | ||
this.name = name; | ||
this.period = period; | ||
} | ||
|
||
public String call() { | ||
|
||
try { | ||
Thread.sleep(period); | ||
} catch (InterruptedException ex) { | ||
// handle exception | ||
ex.printStackTrace(); | ||
} | ||
|
||
return name; | ||
} | ||
} |
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
147 changes: 147 additions & 0 deletions
147
.../src/test/java/com/baeldung/concurrent/executorservice/WaitingForThreadsToFinishTest.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,147 @@ | ||
package com.baeldung.concurrent.executorservice; | ||
|
||
import org.junit.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.concurrent.*; | ||
|
||
import static junit.framework.TestCase.assertTrue; | ||
|
||
public class WaitingForThreadsToFinishTest { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(WaitingForThreadsToFinishTest.class); | ||
private final static ExecutorService WORKER_THREAD_POOL = Executors.newFixedThreadPool(10); | ||
|
||
@Test | ||
public void givenMultipleThreads_whenInvokeAll_thenMainThreadShouldWaitForAllToFinish() { | ||
|
||
ExecutorService WORKER_THREAD_POOL = Executors.newFixedThreadPool(10); | ||
|
||
List<Callable<String>> callables = Arrays.asList(new DelayedCallable("fast thread", 100), new DelayedCallable("slow thread", 3000)); | ||
|
||
try { | ||
long startProcessingTime = System.currentTimeMillis(); | ||
List<Future<String>> futures = WORKER_THREAD_POOL.invokeAll(callables); | ||
|
||
long totalProcessingTime = System.currentTimeMillis() - startProcessingTime; | ||
assertTrue(totalProcessingTime >= 3000); | ||
|
||
String firstThreadResponse = futures.get(0) | ||
.get(); | ||
assertTrue("First response should be from the fast thread", "fast thread".equals(firstThreadResponse)); | ||
|
||
String secondThreadResponse = futures.get(1) | ||
.get(); | ||
assertTrue("Last response should be from the slow thread", "slow thread".equals(secondThreadResponse)); | ||
|
||
} catch (ExecutionException | InterruptedException ex) { | ||
ex.printStackTrace(); | ||
} | ||
|
||
WORKER_THREAD_POOL.shutdown(); | ||
} | ||
|
||
@Test | ||
public void givenMultipleThreads_whenUsingCompletionService_thenMainThreadShouldWaitForAllToFinish() { | ||
|
||
CompletionService<String> service = new ExecutorCompletionService<>(WORKER_THREAD_POOL); | ||
|
||
List<Callable<String>> callables = Arrays.asList(new DelayedCallable("fast thread", 100), new DelayedCallable("slow thread", 3000)); | ||
|
||
for (Callable<String> callable : callables) { | ||
service.submit(callable); | ||
} | ||
|
||
WORKER_THREAD_POOL.shutdown(); | ||
|
||
try { | ||
|
||
long startProcessingTime = System.currentTimeMillis(); | ||
|
||
Future<String> future = service.take(); | ||
String firstThreadResponse = future.get(); | ||
long totalProcessingTime = System.currentTimeMillis() - startProcessingTime; | ||
|
||
assertTrue("First response should be from the fast thread", "fast thread".equals(firstThreadResponse)); | ||
assertTrue(totalProcessingTime >= 100 && totalProcessingTime < 1000); | ||
LOG.debug("Thread finished after: " + totalProcessingTime + " milliseconds"); | ||
|
||
future = service.take(); | ||
String secondThreadResponse = future.get(); | ||
totalProcessingTime = System.currentTimeMillis() - startProcessingTime; | ||
|
||
assertTrue("Last response should be from the slow thread", "slow thread".equals(secondThreadResponse)); | ||
assertTrue(totalProcessingTime >= 3000 && totalProcessingTime < 4000); | ||
LOG.debug("Thread finished after: " + totalProcessingTime + " milliseconds"); | ||
|
||
} catch (ExecutionException | InterruptedException ex) { | ||
ex.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void givenMultipleThreads_whenUsingCompletableFutures_thenMainThreadShouldWaitForAllToFinish() { | ||
|
||
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> { | ||
|
||
try { | ||
Thread.sleep(1000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return "Hello"; | ||
}); | ||
|
||
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> { | ||
|
||
try { | ||
Thread.sleep(5000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return "Beautiful"; | ||
}); | ||
|
||
CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> { | ||
|
||
try { | ||
Thread.sleep(3000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return "World"; | ||
}); | ||
|
||
long startProcessingTime = System.currentTimeMillis(); | ||
CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(future1, future2, future3); | ||
combinedFuture.join(); | ||
|
||
long totalProcessingTime = System.currentTimeMillis() - startProcessingTime; | ||
assertTrue(totalProcessingTime >= 5000 && totalProcessingTime < 6000); | ||
|
||
LOG.debug("Responses from all threads are available after " + totalProcessingTime + " milliseconds"); | ||
|
||
try { | ||
String thread1Response = future1.get(); | ||
assertTrue(thread1Response.equals("Hello")); | ||
|
||
String thread2Response = future2.get(); | ||
assertTrue(thread2Response.equals("Beautiful")); | ||
|
||
String thread3Response = future3.get(); | ||
assertTrue(thread3Response.equals("World")); | ||
|
||
} catch (InterruptedException | ExecutionException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
WORKER_THREAD_POOL.shutdown(); | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
drools/backward-chaining/src/main/java/com/baeldung/drools/BackwardChaining.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,33 @@ | ||
package com.baeldung.drools; | ||
|
||
import org.kie.api.KieServices; | ||
import org.kie.api.runtime.KieContainer; | ||
import org.kie.api.runtime.KieSession; | ||
|
||
import com.baeldung.drools.model.Fact; | ||
import com.baeldung.drools.model.Result; | ||
|
||
public class BackwardChaining { | ||
public static void main(String[] args) { | ||
Result result = new BackwardChaining().backwardChaining(); | ||
System.out.println(result.getValue()); | ||
result.getFacts().stream().forEach(System.out::println); | ||
} | ||
|
||
public Result backwardChaining() { | ||
Result result = new Result(); | ||
KieServices ks = KieServices.Factory.get(); | ||
KieContainer kContainer = ks.getKieClasspathContainer(); | ||
KieSession ksession = kContainer.newKieSession("ksession-backward-chaining"); | ||
ksession.setGlobal("result", result); | ||
ksession.insert(new Fact("Asia", "Planet Earth")); | ||
// ksession.insert(new Fact("China", "Asia")); | ||
ksession.insert(new Fact("Great Wall of China", "China")); | ||
|
||
ksession.fireAllRules(); | ||
|
||
return result; | ||
|
||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
drools/backward-chaining/src/main/java/com/baeldung/drools/model/Fact.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,69 @@ | ||
package com.baeldung.drools.model; | ||
|
||
import org.kie.api.definition.type.Position; | ||
|
||
public class Fact { | ||
|
||
@Position(0) | ||
private String element; | ||
|
||
@Position(1) | ||
private String place; | ||
|
||
public Fact(String element, String place) { | ||
this.element = element; | ||
this.place = place; | ||
} | ||
|
||
public String getElement() { | ||
return element; | ||
} | ||
|
||
public void setElement(String element) { | ||
this.element = element; | ||
} | ||
|
||
public String getPlace() { | ||
return place; | ||
} | ||
|
||
public void setPlace(String place) { | ||
this.place = place; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ((element == null) ? 0 : element.hashCode()); | ||
result = prime * result + ((place == null) ? 0 : place.hashCode()); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
Fact other = (Fact) obj; | ||
if (element == null) { | ||
if (other.element != null) | ||
return false; | ||
} else if (!element.equals(other.element)) | ||
return false; | ||
if (place == null) { | ||
if (other.place != null) | ||
return false; | ||
} else if (!place.equals(other.place)) | ||
return false; | ||
return true; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Fact{" + "element='" + element + '\'' + ", place='" + place + '\'' + '}'; | ||
} | ||
} |
Oops, something went wrong.