23
23
24
24
package com .iluwatar .event .asynchronous ;
25
25
26
- import org .slf4j .Logger ;
27
- import org .slf4j .LoggerFactory ;
28
-
29
26
import java .io .IOException ;
30
27
import java .io .InputStream ;
31
28
import java .util .Properties ;
32
29
import java .util .Scanner ;
30
+ import org .slf4j .Logger ;
31
+ import org .slf4j .LoggerFactory ;
33
32
34
33
/**
34
+ * This application demonstrates the <b>Event-based Asynchronous</b> pattern. Essentially, users (of
35
+ * the pattern) may choose to run events in an Asynchronous or Synchronous mode. There can be
36
+ * multiple Asynchronous events running at once but only one Synchronous event can run at a time.
37
+ * Asynchronous events are synonymous to multi-threads. The key point here is that the threads run
38
+ * in the background and the user is free to carry on with other processes. Once an event is
39
+ * complete, the appropriate listener/callback method will be called. The listener then proceeds to
40
+ * carry out further processing depending on the needs of the user.
35
41
*
36
- * This application demonstrates the <b>Event-based Asynchronous</b> pattern. Essentially, users (of the pattern) may
37
- * choose to run events in an Asynchronous or Synchronous mode. There can be multiple Asynchronous events running at
38
- * once but only one Synchronous event can run at a time. Asynchronous events are synonymous to multi-threads. The key
39
- * point here is that the threads run in the background and the user is free to carry on with other processes. Once an
40
- * event is complete, the appropriate listener/callback method will be called. The listener then proceeds to carry out
41
- * further processing depending on the needs of the user.
42
+ * <p>The {@link EventManager} manages the events/threads that the user creates. Currently, the
43
+ * supported event operations are: <code>start</code>, <code>stop</code>, <code>getStatus</code>.
44
+ * For Synchronous events, the user is unable to start another (Synchronous) event if one is already
45
+ * running at the time. The running event would have to either be stopped or completed before a new
46
+ * event can be started.
42
47
*
43
- * The {@link EventManager} manages the events/threads that the user creates. Currently, the supported event operations
44
- * are: <code>start</code>, <code>stop</code>, <code>getStatus</code>. For Synchronous events, the user is unable to
45
- * start another (Synchronous) event if one is already running at the time. The running event would have to either be
46
- * stopped or completed before a new event can be started.
47
- *
48
- * The Event-based Asynchronous Pattern makes available the advantages of multithreaded applications while hiding many
49
- * of the complex issues inherent in multithreaded design. Using a class that supports this pattern can allow you to:-
50
- * (1) Perform time-consuming tasks, such as downloads and database operations, "in the background," without
51
- * interrupting your application. (2) Execute multiple operations simultaneously, receiving notifications when each
52
- * completes. (3) Wait for resources to become available without stopping ("hanging") your application. (4) Communicate
53
- * with pending asynchronous operations using the familiar events-and-delegates model.
48
+ * <p>The Event-based Asynchronous Pattern makes available the advantages of multithreaded
49
+ * applications while hiding many of the complex issues inherent in multithreaded design. Using a
50
+ * class that supports this pattern can allow you to:- (1) Perform time-consuming tasks, such as
51
+ * downloads and database operations, "in the background," without interrupting your application.
52
+ * (2) Execute multiple operations simultaneously, receiving notifications when each completes. (3)
53
+ * Wait for resources to become available without stopping ("hanging") your application. (4)
54
+ * Communicate with pending asynchronous operations using the familiar events-and-delegates model.
54
55
*
55
56
* @see EventManager
56
57
* @see Event
57
- *
58
58
*/
59
59
public class App {
60
60
@@ -67,8 +67,7 @@ public class App {
67
67
/**
68
68
* Program entry point.
69
69
*
70
- * @param args
71
- * command line args
70
+ * @param args command line args
72
71
*/
73
72
public static void main (String [] args ) {
74
73
App app = new App ();
@@ -78,8 +77,9 @@ public static void main(String[] args) {
78
77
}
79
78
80
79
/**
81
- * App can run in interactive mode or not. Interactive mode == Allow user interaction with command line.
82
- * Non-interactive is a quick sequential run through the available {@link EventManager} operations.
80
+ * App can run in interactive mode or not. Interactive mode == Allow user interaction with command
81
+ * line. Non-interactive is a quick sequential run through the available {@link EventManager}
82
+ * operations.
83
83
*/
84
84
public void setUp () {
85
85
Properties prop = new Properties ();
@@ -118,24 +118,24 @@ public void quickRun() {
118
118
119
119
try {
120
120
// Create an Asynchronous event.
121
- int aEventId = eventManager .createAsync (60 );
122
- LOGGER .info ("Async Event [{}] has been created." , aEventId );
123
- eventManager .start (aEventId );
124
- LOGGER .info ("Async Event [{}] has been started." , aEventId );
121
+ int asyncEventId = eventManager .createAsync (60 );
122
+ LOGGER .info ("Async Event [{}] has been created." , asyncEventId );
123
+ eventManager .start (asyncEventId );
124
+ LOGGER .info ("Async Event [{}] has been started." , asyncEventId );
125
125
126
126
// Create a Synchronous event.
127
- int sEventId = eventManager .create (60 );
128
- LOGGER .info ("Sync Event [{}] has been created." , sEventId );
129
- eventManager .start (sEventId );
130
- LOGGER .info ("Sync Event [{}] has been started." , sEventId );
127
+ int syncEventId = eventManager .create (60 );
128
+ LOGGER .info ("Sync Event [{}] has been created." , syncEventId );
129
+ eventManager .start (syncEventId );
130
+ LOGGER .info ("Sync Event [{}] has been started." , syncEventId );
131
131
132
- eventManager .status (aEventId );
133
- eventManager .status (sEventId );
132
+ eventManager .status (asyncEventId );
133
+ eventManager .status (syncEventId );
134
134
135
- eventManager .cancel (aEventId );
136
- LOGGER .info ("Async Event [{}] has been stopped." , aEventId );
137
- eventManager .cancel (sEventId );
138
- LOGGER .info ("Sync Event [{}] has been stopped." , sEventId );
135
+ eventManager .cancel (asyncEventId );
136
+ LOGGER .info ("Async Event [{}] has been stopped." , asyncEventId );
137
+ eventManager .cancel (syncEventId );
138
+ LOGGER .info ("Sync Event [{}] has been stopped." , syncEventId );
139
139
140
140
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException
141
141
| InvalidOperationException e ) {
@@ -211,16 +211,17 @@ private void processOption1(EventManager eventManager, Scanner s) {
211
211
int eventId = eventManager .createAsync (eventTime );
212
212
eventManager .start (eventId );
213
213
LOGGER .info ("Egg [{}] is being boiled." , eventId );
214
- } catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e ) {
214
+ } catch (MaxNumOfEventsAllowedException | LongRunningEventException
215
+ | EventDoesNotExistException e ) {
215
216
LOGGER .error (e .getMessage ());
216
217
}
217
218
} else if (eventType .equalsIgnoreCase ("S" )) {
218
219
try {
219
220
int eventId = eventManager .create (eventTime );
220
221
eventManager .start (eventId );
221
222
LOGGER .info ("Egg [{}] is being boiled." , eventId );
222
- } catch (MaxNumOfEventsAllowedException | InvalidOperationException | LongRunningEventException
223
- | EventDoesNotExistException e ) {
223
+ } catch (MaxNumOfEventsAllowedException | InvalidOperationException
224
+ | LongRunningEventException | EventDoesNotExistException e ) {
224
225
LOGGER .error (e .getMessage ());
225
226
}
226
227
} else {
0 commit comments