-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04-executors.cfc
168 lines (143 loc) · 4.15 KB
/
04-executors.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* =================================================================================
* THIS IS THE SOLUTION FILE. TO WORK ON THE EXERCISES GO TO THE `exercises` FOLDER.
* =================================================================================
*
* This exercise goes over:
* + Creating executors
* + Using a custom executor for a Future
* + Cancelling running threads when cancelling a Future.
*
* You can run this exercise from the root in CommandBox by running:
* `task run exercises/04-executors.cfc`
*
* You can use the `print` helper provided by CommandBox to log messages.
* (Use `print.line( "message" ).toConsole()` if you're not familiar with it.)
* Make sure to end with `.toConsole()` so your message is flushed
* to the console the moment it is printed.
*/
component extends="../BaseTask" {
/**
* Create 10 futures that prints the current time after a 1 second sleep.
* Observe how frequent the futures complete.
*/
function partOne(){
var futures = [];
for ( var i = 1; i <= 10; i++ ) {
futures.append(
asyncManager.newFuture( () => {
sleep( 1000 );
print.line( dateTimeFormat( now(), "full" ) ).toConsole();
} )
);
}
for ( var future in futures ) {
future.get();
}
return asyncManager.newCompletedFuture( true );
}
/**
* Create a custom single executor
* Create 10 futures that prints the current time after a 1 second sleep.
* Run those futures using the custom single exector you created.
* Observe how frequent the futures complete.
*/
function partTwo(){
var executor = asyncManager.newSingleExecutor( "custom-single-executor" );
var futures = [];
for ( var i = 1; i <= 10; i++ ) {
futures.append(
asyncManager.newFuture( () => {
sleep( 1000 );
print.line( dateTimeFormat( now(), "full" ) ).toConsole();
}, executor )
);
}
for ( var future in futures ) {
future.get();
}
return asyncManager.newCompletedFuture( true );
}
/**
* Create a custom fixed executor with 4 threads.
* Create 10 futures that prints the current time after a 1 second sleep.
* Run those futures using the custom fixed exector you created.
* Observe how frequent the futures complete.
*/
function partThree(){
var executor = asyncManager.newExecutor( "custom-fixed-executor", "fixed", 4 );
var futures = [];
for ( var i = 1; i <= 10; i++ ) {
futures.append(
asyncManager.newFuture( () => {
sleep( 1000 );
print.line( dateTimeFormat( now(), "full" ) ).toConsole();
}, executor )
);
}
for ( var future in futures ) {
future.get();
}
return asyncManager.newCompletedFuture( true );
}
/**
* Create a future that prints the current time after a 5 second sleep.
* After 2 seconds, cancel the future.
* Question: what do you see in the console?
*/
function partFour(){
var future = asyncManager.newFuture( () => {
sleep( 5000 );
print.line( dateTimeFormat( now(), "full" ) ).toConsole();
} );
sleep( 2000 );
future.cancel();
return asyncManager.newCompletedFuture( true );
}
/**
* Create a custom executor.
* Create a future that prints the current time after a 5 second sleep.
* Run that future using the custom exector you created.
* After 2 seconds, cancel the future and shutdown the custom executor.
* Question: what do you see in the console?
*/
function partFive(){
var executor = asyncManager.newExecutor( "custom-cancellable-executor" );
var future = asyncManager.newFuture( () => {
sleep( 5000 );
print.line( dateTimeFormat( now(), "full" ) ).toConsole();
}, executor );
sleep( 2000 );
future.cancel();
executor.shutdownNow();
return asyncManager.newCompletedFuture( true );
}
function run(){
print.blueLine( "Running all `04-executors` exercises" ).toConsole();
print
.line()
.line( "Part One" )
.toConsole();
partOne().get();
print
.line()
.line( "Part Two" )
.toConsole();
partTwo().get();
print
.line()
.line( "Part Three" )
.toConsole();
partThree().get();
print
.line()
.line( "Part Four" )
.toConsole();
partFour().get();
print
.line()
.line( "Part Five" )
.toConsole();
partFive().get();
}
}