3030
3131package org .scijava .ui .swing .console ;
3232
33+ import java .util .concurrent .Future ;
34+
3335import org .scijava .Context ;
3436import org .scijava .thread .ThreadService ;
3537import org .scijava .ui .UIService ;
@@ -48,20 +50,61 @@ public static void main(final String[] args) throws Exception {
4850 final Context context = new Context ();
4951 context .service (UIService .class ).showUI ();
5052
51- System .out .println ("Hello!" );
53+ System .out .print ("Hello " );
54+ System .err .println ("world!" );
55+
56+ final int numThreads = 50 ;
57+ final int numOperations = 20 ;
58+
59+ final String [] streamLabels =
60+ { ": {ERR} iteration #" , ": {OUT} iteration #" };
61+ final String outLabel = streamLabels [1 ];
62+ final String errLabel = streamLabels [0 ];
63+
64+ final int initialDelay = 500 ;
65+
66+ Thread .sleep (initialDelay );
67+
68+ final long start = System .currentTimeMillis ();
5269
70+ // emit a bunch of output on multiple threads concurrently
5371 final ThreadService threadService = context .service (ThreadService .class );
54- threadService .run (new Runnable () {
72+ final Future <?>[] f = new Future <?>[numThreads ];
73+ for (int t = 0 ; t < numThreads ; t ++) {
74+ final int tNo = t ;
75+ f [t ] = threadService .run (new Runnable () {
5576
56- @ Override
57- public void run () {
58- System .out .println ("This is a test of the emergency console system." );
59- System .err .println ("In a real emergency, your computer would explode." );
60- }
77+ @ Override
78+ public void run () {
79+ for (int i = 0 ; i < numOperations ; i ++) {
80+ System .out .print (str (tNo , outLabel , i ) + "\n " );
81+ System .err .print (str (tNo , errLabel , i ) + "\n " );
82+ }
83+ }
84+ });
85+ }
6186
62- }).get ();
87+ // wait for all output threads to finish
88+ for (int t = 0 ; t < numThreads ; t ++) {
89+ f [t ].get ();
90+ }
91+
92+ System .err .print ("Goodbye " );
93+ System .out .println ("cruel world!" );
94+
95+ final long end = System .currentTimeMillis ();
96+ System .out .println ();
97+ System .out .println ("Benchmark took " + (end - start ) + " ms" );
98+ }
99+
100+ // - Helper methods --
101+
102+ private static String str (final int t , final String separator , final int i ) {
103+ return pad (t ) + separator + pad (i );
104+ }
63105
64- System .err .println ("Goodbye!" );
106+ private static String pad (final int n ) {
107+ return n < 10 ? "0" + n : "" + n ;
65108 }
66109
67110}
0 commit comments