Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,17 @@
package com.iluwatar.templatemethod;

/**
*
* Template Method defines a skeleton for an algorithm. The algorithm subclasses provide
* implementation for the blank parts.
* <p>
* In this example {@link HalflingThief} contains {@link StealingMethod} that can be changed. First
* the thief hits with {@link HitAndRunMethod} and then with {@link SubtleMethod}.
*
*
* <p>In this example {@link HalflingThief} contains {@link StealingMethod} that can be changed.
* First the thief hits with {@link HitAndRunMethod} and then with {@link SubtleMethod}.
*/
public class App {

/**
* Program entry point
*
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package com.iluwatar.templatemethod;

/**
*
* Halfling thief uses {@link StealingMethod} to steal.
*
*/
public class HalflingThief {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import org.slf4j.LoggerFactory;

/**
*
* HitAndRunMethod implementation of {@link StealingMethod}.
*
*/
public class HitAndRunMethod extends StealingMethod {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import org.slf4j.LoggerFactory;

/**
*
* StealingMethod defines skeleton for the algorithm.
*
*/
public abstract class StealingMethod {

Expand All @@ -42,7 +40,7 @@ public abstract class StealingMethod {
protected abstract void stealTheItem(String target);

/**
* Steal
* Steal.
*/
public void steal() {
var target = pickTarget();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import org.slf4j.LoggerFactory;

/**
*
* SubtleMethod implementation of {@link StealingMethod}.
*
*/
public class SubtleMethod extends StealingMethod {

Expand Down
52 changes: 24 additions & 28 deletions thread-pool/src/main/java/com/iluwatar/threadpool/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,32 @@

package com.iluwatar.threadpool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* Thread Pool pattern is where a number of threads are created to perform a number of tasks, which
* are usually organized in a queue. The results from the tasks being executed might also be placed
* in a queue, or the tasks might return no result. Typically, there are many more tasks than
* threads. As soon as a thread completes its task, it will request the next task from the queue
* until all tasks have been completed. The thread can then terminate, or sleep until there are new
* tasks available.
* <p>
* In this example we create a list of tasks presenting work to be done. Each task is then wrapped
* into a {@link Worker} object that implements {@link Runnable}. We create an
* {@link ExecutorService} with fixed number of threads (Thread Pool) and use them to execute the
* {@link Worker}s.
*
* <p>In this example we create a list of tasks presenting work to be done. Each task is then
* wrapped into a {@link Worker} object that implements {@link Runnable}. We create an {@link
* ExecutorService} with fixed number of threads (Thread Pool) and use them to execute the {@link
* Worker}s.
*/
public class App {

private static final Logger LOGGER = LoggerFactory.getLogger(App.class);

/**
* Program entry point
*
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) {
Expand All @@ -61,21 +57,21 @@ public static void main(String[] args) {

// Create a list of tasks to be executed
List<Task> tasks = List.of(
new PotatoPeelingTask(3),
new PotatoPeelingTask(6),
new CoffeeMakingTask(2),
new CoffeeMakingTask(6),
new PotatoPeelingTask(4),
new CoffeeMakingTask(2),
new PotatoPeelingTask(4),
new CoffeeMakingTask(9),
new PotatoPeelingTask(3),
new CoffeeMakingTask(2),
new PotatoPeelingTask(4),
new CoffeeMakingTask(2),
new CoffeeMakingTask(7),
new PotatoPeelingTask(4),
new PotatoPeelingTask(5));
new PotatoPeelingTask(3),
new PotatoPeelingTask(6),
new CoffeeMakingTask(2),
new CoffeeMakingTask(6),
new PotatoPeelingTask(4),
new CoffeeMakingTask(2),
new PotatoPeelingTask(4),
new CoffeeMakingTask(9),
new PotatoPeelingTask(3),
new CoffeeMakingTask(2),
new PotatoPeelingTask(4),
new CoffeeMakingTask(2),
new CoffeeMakingTask(7),
new PotatoPeelingTask(4),
new PotatoPeelingTask(5));

// Creates a thread pool that reuses a fixed number of threads operating off a shared
// unbounded queue. At any point, at most nThreads threads will be active processing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package com.iluwatar.threadpool;

/**
*
* CoffeeMakingTask is a concrete task
*
* CoffeeMakingTask is a concrete task.
*/
public class CoffeeMakingTask extends Task {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package com.iluwatar.threadpool;

/**
*
* PotatoPeelingTask is a concrete task
*
* PotatoPeelingTask is a concrete task.
*/
public class PotatoPeelingTask extends Task {

Expand Down
4 changes: 1 addition & 3 deletions thread-pool/src/main/java/com/iluwatar/threadpool/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
import java.util.concurrent.atomic.AtomicInteger;

/**
*
* Abstract base class for tasks
*
* Abstract base class for tasks.
*/
public abstract class Task {

Expand Down
5 changes: 2 additions & 3 deletions thread-pool/src/main/java/com/iluwatar/threadpool/Worker.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
import org.slf4j.LoggerFactory;

/**
*
* Worker implements {@link Runnable} and thus can be executed by {@link java.util.concurrent.ExecutorService}
*
* Worker implements {@link Runnable} and thus can be executed by {@link
* java.util.concurrent.ExecutorService}.
*/
public class Worker implements Runnable {

Expand Down
33 changes: 15 additions & 18 deletions throttling/src/main/java/com/iluwatar/throttling/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,30 @@

package com.iluwatar.throttling;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iluwatar.throttling.timer.Throttler;
import com.iluwatar.throttling.timer.ThrottleTimerImpl;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Throttling pattern is a design pattern to throttle or limit the use of resources or even a complete service by
* users or a particular tenant. This can allow systems to continue to function and meet service level agreements,
* even when an increase in demand places load on resources.
* Throttling pattern is a design pattern to throttle or limit the use of resources or even a
* complete service by users or a particular tenant. This can allow systems to continue to function
* and meet service level agreements, even when an increase in demand places load on resources.
* <p>
* In this example we have ({@link App}) as the initiating point of the service.
* This is a time based throttling, i.e. only a certain number of calls are allowed per second.
* In this example we have ({@link App}) as the initiating point of the service. This is a time
* based throttling, i.e. only a certain number of calls are allowed per second.
* </p>
* ({@link Tenant}) is the Tenant POJO class with which many tenants can be created
* ({@link B2BService}) is the service which is consumed by the tenants and is throttled.
* ({@link Tenant}) is the Tenant POJO class with which many tenants can be created ({@link
* B2BService}) is the service which is consumed by the tenants and is throttled.
*/
public class App {

private static final Logger LOGGER = LoggerFactory.getLogger(App.class);

/**
* Application entry point
* Application entry point.
*
* @param args main arguments
*/
public static void main(String[] args) {
Expand All @@ -58,10 +55,10 @@ public static void main(String[] args) {
var nike = new Tenant("Nike", 6, callsCount);

var executorService = Executors.newFixedThreadPool(2);

executorService.execute(() -> makeServiceCalls(adidas, callsCount));
executorService.execute(() -> makeServiceCalls(nike, callsCount));

executorService.shutdown();
try {
executorService.awaitTermination(10, TimeUnit.SECONDS);
Expand All @@ -71,14 +68,14 @@ public static void main(String[] args) {
}

/**
* Make calls to the B2BService dummy API
* Make calls to the B2BService dummy API.
*/
private static void makeServiceCalls(Tenant tenant, CallsCount callsCount) {
var timer = new ThrottleTimerImpl(10, callsCount);
var service = new B2BService(timer, callsCount);
for (int i = 0; i < 20; i++) {
service.dummyCustomerApi(tenant);
// Sleep is introduced to keep the output in check and easy to view and analyze the results.
// Sleep is introduced to keep the output in check and easy to view and analyze the results.
try {
Thread.sleep(1);
} catch (InterruptedException e) {
Expand Down
10 changes: 5 additions & 5 deletions throttling/src/main/java/com/iluwatar/throttling/B2BService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@

package com.iluwatar.throttling;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iluwatar.throttling.timer.Throttler;

import java.util.concurrent.ThreadLocalRandom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A service which accepts a tenant and throttles the resource based on the time given to the tenant.
* A service which accepts a tenant and throttles the resource based on the time given to the
* tenant.
*/
class B2BService {

Expand All @@ -44,6 +43,7 @@ public B2BService(Throttler timer, CallsCount callsCount) {
}

/**
* Calls dummy customer api.
*
* @return customer id which is randomly generated
*/
Expand Down
20 changes: 11 additions & 9 deletions throttling/src/main/java/com/iluwatar/throttling/CallsCount.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@

package com.iluwatar.throttling;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A class to keep track of the counter of different Tenants
* @author drastogi
* A class to keep track of the counter of different Tenants.
*
* @author drastogi
*/
public final class CallsCount {

Expand All @@ -43,29 +42,32 @@ public final class CallsCount {

/**
* Add a new tenant to the map.
*
* @param tenantName name of the tenant.
*/
public void addTenant(String tenantName) {
tenantCallsCount.putIfAbsent(tenantName, new AtomicLong(0));
}

/**
* Increment the count of the specified tenant.
*
* @param tenantName name of the tenant.
*/
public void incrementCount(String tenantName) {
tenantCallsCount.get(tenantName).incrementAndGet();
}

/**
*
* Get count of tenant based on tenant name.
*
* @param tenantName name of the tenant.
* @return the count of the tenant.
*/
public long getCount(String tenantName) {
return tenantCallsCount.get(tenantName).get();
}

/**
* Resets the count of all the tenants in the map.
*/
Expand Down
3 changes: 2 additions & 1 deletion throttling/src/main/java/com/iluwatar/throttling/Tenant.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public class Tenant {
private int allowedCallsPerSecond;

/**
* Constructor.
*
* @param name Name of the tenant
* @param name Name of the tenant
* @param allowedCallsPerSecond The number of calls allowed for a particular tenant.
* @throws InvalidParameterException If number of calls is less than 0, throws exception.
*/
Expand Down
Loading