Skip to content

Commit d2071aa

Browse files
committed
clean code: main function style
1 parent 695d47f commit d2071aa

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

show-busy-java-threads

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ cleanupWhenExit() {
230230
}
231231
trap "cleanupWhenExit" EXIT
232232

233+
233234
headInfo() {
234235
colorEcho "0;34;42" ================================================================================
235236
echo "$(date "+%Y-%m-%d %H:%M:%S.%N") [$(( i + 1 ))/$update_count]: ${COMMAND_LINE[@]}"
@@ -271,7 +272,7 @@ __top_threadId_cpu() {
271272
# only print 4th text block(idx == 3)
272273
# aka. process info of second top update
273274
print $1 " " $9 # $1 is thread id, $9 is %cpu
274-
if($0 == "")
275+
if ($0 == "")
275276
idx++
276277
}' | sort -k2,2nr
277278
}
@@ -359,16 +360,25 @@ printStackOfThreads() {
359360
done
360361
}
361362

362-
# if update_count <= 0, infinite loop till user interrupted (eg: CTRL+C)
363-
for (( i = 0; update_count <= 0 || i < update_count; ++i )); do
364-
(( i > 0 )) && sleep "$update_delay"
363+
################################################################################
364+
# Main
365+
################################################################################
365366

366-
[ -n "$append_file" ] && headInfo >> "$append_file"
367-
(( update_count != 1 )) && headInfo
367+
main() {
368+
local i
369+
# if update_count <= 0, infinite loop till user interrupted (eg: CTRL+C)
370+
for (( i = 0; update_count <= 0 || i < update_count; ++i )); do
371+
(( i > 0 )) && sleep "$update_delay"
368372

369-
if $use_ps; then
370-
findBusyJavaThreadsByPs
371-
else
372-
findBusyJavaThreadsByTop
373-
fi | printStackOfThreads $(( i + 1 ))
374-
done
373+
[ -n "$append_file" ] && headInfo >> "$append_file"
374+
(( update_count != 1 )) && headInfo
375+
376+
if $use_ps; then
377+
findBusyJavaThreadsByPs
378+
else
379+
findBusyJavaThreadsByTop
380+
fi | printStackOfThreads $(( i + 1 ))
381+
done
382+
}
383+
384+
main

0 commit comments

Comments
 (0)