Resolves checkstyle errors for guarded-suspension, half-sync-half-async, hexagonal (#1064)
* Reduces checkstyle errors in guarded-suspension * Reduces checkstyle errors in half-sync-half-async * Reduces checkstyle errors in hexagonal
This commit is contained in:
committed by
Ilkka Seppälä
parent
4f9ee0189c
commit
dda09535e6
@ -23,42 +23,35 @@
|
||||
|
||||
package com.iluwatar.halfsynchalfasync;
|
||||
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
/**
|
||||
*
|
||||
* This application demonstrates Half-Sync/Half-Async pattern. Key parts of the pattern are
|
||||
* {@link AsyncTask} and {@link AsynchronousService}.
|
||||
*
|
||||
* <p>
|
||||
* <i>PROBLEM</i> <br>
|
||||
* This application demonstrates Half-Sync/Half-Async pattern. Key parts of the pattern are {@link
|
||||
* AsyncTask} and {@link AsynchronousService}.
|
||||
*
|
||||
* <p><i>PROBLEM</i> <br>
|
||||
* A concurrent system have a mixture of short duration, mid duration and long duration tasks. Mid
|
||||
* or long duration tasks should be performed asynchronously to meet quality of service
|
||||
* requirements.
|
||||
*
|
||||
* <p>
|
||||
* <i>INTENT</i> <br>
|
||||
*
|
||||
* <p><i>INTENT</i> <br>
|
||||
* The intent of this pattern is to separate the the synchronous and asynchronous processing in the
|
||||
* concurrent application by introducing two intercommunicating layers - one for sync and one for
|
||||
* async. This simplifies the programming without unduly affecting the performance.
|
||||
*
|
||||
* <p>
|
||||
* <i>APPLICABILITY</i> <br>
|
||||
* UNIX network subsystems - In operating systems network operations are carried out
|
||||
* asynchronously with help of hardware level interrupts.<br>
|
||||
* CORBA - At the asynchronous layer one thread is associated with each socket that is connected
|
||||
* to the client. Thread blocks waiting for CORBA requests from the client. On receiving request it
|
||||
* is inserted in the queuing layer which is then picked up by synchronous layer which processes the
|
||||
* request and sends response back to the client.<br>
|
||||
* Android AsyncTask framework - Framework provides a way to execute long running blocking
|
||||
* calls, such as downloading a file, in background threads so that the UI thread remains free to
|
||||
* respond to user inputs.<br>
|
||||
*
|
||||
* <p>
|
||||
* <i>IMPLEMENTATION</i> <br>
|
||||
*
|
||||
* <p><i>APPLICABILITY</i> <br>
|
||||
* UNIX network subsystems - In operating systems network operations are carried out asynchronously
|
||||
* with help of hardware level interrupts.<br> CORBA - At the asynchronous layer one thread is
|
||||
* associated with each socket that is connected to the client. Thread blocks waiting for CORBA
|
||||
* requests from the client. On receiving request it is inserted in the queuing layer which is then
|
||||
* picked up by synchronous layer which processes the request and sends response back to the
|
||||
* client.<br> Android AsyncTask framework - Framework provides a way to execute long running
|
||||
* blocking calls, such as downloading a file, in background threads so that the UI thread remains
|
||||
* free to respond to user inputs.<br>
|
||||
*
|
||||
* <p><i>IMPLEMENTATION</i> <br>
|
||||
* The main method creates an asynchronous service which does not block the main thread while the
|
||||
* task is being performed. The main thread continues its work which is similar to Async Method
|
||||
* Invocation pattern. The difference between them is that there is a queuing layer between
|
||||
@ -66,15 +59,14 @@ import java.util.concurrent.LinkedBlockingQueue;
|
||||
* between both layers. Such as Priority Queue can be used as queuing layer to prioritize the way
|
||||
* tasks are executed. Our implementation is just one simple way of implementing this pattern, there
|
||||
* are many variants possible as described in its applications.
|
||||
*
|
||||
*/
|
||||
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) {
|
||||
@ -100,15 +92,13 @@ public class App {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* ArithmeticSumTask
|
||||
*
|
||||
* ArithmeticSumTask.
|
||||
*/
|
||||
static class ArithmeticSumTask implements AsyncTask<Long> {
|
||||
private long n;
|
||||
private long numberOfElements;
|
||||
|
||||
public ArithmeticSumTask(long n) {
|
||||
this.n = n;
|
||||
public ArithmeticSumTask(long numberOfElements) {
|
||||
this.numberOfElements = numberOfElements;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -117,7 +107,7 @@ public class App {
|
||||
*/
|
||||
@Override
|
||||
public Long call() throws Exception {
|
||||
return ap(n);
|
||||
return ap(numberOfElements);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -128,7 +118,7 @@ public class App {
|
||||
*/
|
||||
@Override
|
||||
public void onPreCall() {
|
||||
if (n < 0) {
|
||||
if (numberOfElements < 0) {
|
||||
throw new IllegalArgumentException("n is less than 0");
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ import java.util.concurrent.Callable;
|
||||
* typically done is background threads and the result is posted back in form of callback. The
|
||||
* callback does not implement {@code isComplete}, {@code cancel} as it is out of scope of this
|
||||
* pattern.
|
||||
*
|
||||
*
|
||||
* @param <O> type of result
|
||||
*/
|
||||
public interface AsyncTask<O> extends Callable<O> {
|
||||
@ -53,7 +53,7 @@ public interface AsyncTask<O> extends Callable<O> {
|
||||
/**
|
||||
* A callback called if computing the task resulted in some exception. This method is called when
|
||||
* either of {@link #call()} or {@link #onPreCall()} throw any exception.
|
||||
*
|
||||
*
|
||||
* @param throwable error cause
|
||||
*/
|
||||
void onError(Throwable throwable);
|
||||
|
@ -23,15 +23,14 @@
|
||||
|
||||
package com.iluwatar.halfsynchalfasync;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.FutureTask;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* This is the asynchronous layer which does not block when a new request arrives. It just passes
|
||||
@ -63,13 +62,14 @@ public class AsynchronousService {
|
||||
|
||||
/**
|
||||
* A non-blocking method which performs the task provided in background and returns immediately.
|
||||
* <p>
|
||||
* On successful completion of task the result is posted back using callback method
|
||||
* {@link AsyncTask#onPostCall(Object)}, if task execution is unable to complete normally due to
|
||||
* some exception then the reason for error is posted back using callback method
|
||||
* {@link AsyncTask#onError(Throwable)}.
|
||||
* <p>
|
||||
* NOTE: The results are posted back in the context of background thread in this implementation.
|
||||
*
|
||||
* <p>On successful completion of task the result is posted back using callback method {@link
|
||||
* AsyncTask#onPostCall(Object)}, if task execution is unable to complete normally due to some
|
||||
* exception then the reason for error is posted back using callback method {@link
|
||||
* AsyncTask#onError(Throwable)}.
|
||||
*
|
||||
* <p>NOTE: The results are posted back in the context of background thread in this
|
||||
* implementation.
|
||||
*/
|
||||
public <T> void execute(final AsyncTask<T> task) {
|
||||
try {
|
||||
|
Reference in New Issue
Block a user