@@ -39,7 +39,7 @@ public class AppendOutputRunner implements Runnable {
3939 LoggerFactory .getLogger (AppendOutputRunner .class );
4040 private static RemoteInterpreterProcessListener listener ;
4141
42- private static final Long BUFFER_TIME_MS = new Long (100 );
42+ public static final Long BUFFER_TIME_MS = new Long (100 );
4343 private static final Long SAFE_PROCESSING_TIME = new Long (10 );
4444 private static final Long SAFE_PROCESSING_STRING_SIZE = new Long (100000 );
4545
@@ -49,74 +49,65 @@ public class AppendOutputRunner implements Runnable {
4949 @ Override
5050 public void run () {
5151
52- while (true ) {
53- Map <String , Map <String , StringBuilder > > noteMap =
54- new HashMap <String , Map <String , StringBuilder > >();
55- List <AppendOutputBuffer > list = new LinkedList <AppendOutputBuffer >();
56-
57- /* "drainTo" method does not wait for any element
58- * to be present in the queue, and thus this loop would
59- * continuosly run (with period of BUFFER_TIME_MS). "take()" method
60- * waits for the queue to become non-empty and then removes
61- * one element from it. Rest elements from queue (if present) are
62- * removed using "drainTo" method. Thus we save on some un-necessary
63- * cpu-cycles.
64- */
65- try {
66- list .add (QUEUE .take ());
67- } catch (InterruptedException e ) {
68- logger .error ("Wait for OutputBuffer queue interrupted: " + e .getMessage ());
69- break ;
70- }
71- Long processingStartTime = System .currentTimeMillis ();
72- QUEUE .drainTo (list );
52+ Map <String , Map <String , StringBuilder > > noteMap =
53+ new HashMap <String , Map <String , StringBuilder > >();
54+ List <AppendOutputBuffer > list = new LinkedList <AppendOutputBuffer >();
7355
74- for (AppendOutputBuffer buffer : list ) {
75- String noteId = buffer .getNoteId ();
76- String paragraphId = buffer .getParagraphId ();
56+ /* "drainTo" method does not wait for any element
57+ * to be present in the queue, and thus this loop would
58+ * continuosly run (with period of BUFFER_TIME_MS). "take()" method
59+ * waits for the queue to become non-empty and then removes
60+ * one element from it. Rest elements from queue (if present) are
61+ * removed using "drainTo" method. Thus we save on some un-necessary
62+ * cpu-cycles.
63+ */
64+ try {
65+ list .add (QUEUE .take ());
66+ } catch (InterruptedException e ) {
67+ logger .error ("Wait for OutputBuffer queue interrupted: " + e .getMessage ());
68+ }
69+ Long processingStartTime = System .currentTimeMillis ();
70+ QUEUE .drainTo (list );
7771
78- Map <String , StringBuilder > paragraphMap = (noteMap .containsKey (noteId )) ?
79- noteMap .get (noteId ) : new HashMap <String , StringBuilder >();
80- StringBuilder builder = paragraphMap .containsKey (paragraphId ) ?
81- paragraphMap .get (paragraphId ) : new StringBuilder ();
72+ for (AppendOutputBuffer buffer : list ) {
73+ String noteId = buffer .getNoteId ();
74+ String paragraphId = buffer .getParagraphId ();
8275
83- builder .append (buffer .getData ());
84- paragraphMap .put (paragraphId , builder );
85- noteMap .put (noteId , paragraphMap );
86- }
87- Long processingTime = System .currentTimeMillis () - processingStartTime ;
88-
89- if (processingTime > SAFE_PROCESSING_TIME ) {
90- logger .warn ("Processing time for buffered append-output is high: " +
91- processingTime + " milliseconds." );
92- } else {
93- logger .debug ("Processing time for append-output took "
94- + processingTime + " milliseconds" );
95- }
76+ Map <String , StringBuilder > paragraphMap = (noteMap .containsKey (noteId )) ?
77+ noteMap .get (noteId ) : new HashMap <String , StringBuilder >();
78+ StringBuilder builder = paragraphMap .containsKey (paragraphId ) ?
79+ paragraphMap .get (paragraphId ) : new StringBuilder ();
9680
97- Long sizeProcessed = new Long (0 );
98- for (String noteId : noteMap .keySet ()) {
99- for (String paragraphId : noteMap .get (noteId ).keySet ()) {
100- String data = noteMap .get (noteId ).get (paragraphId ).toString ();
101- sizeProcessed += data .length ();
102- listener .onOutputAppend (noteId , paragraphId , data );
103- }
104- }
81+ builder .append (buffer .getData ());
82+ paragraphMap .put (paragraphId , builder );
83+ noteMap .put (noteId , paragraphMap );
84+ }
85+ Long processingTime = System .currentTimeMillis () - processingStartTime ;
86+
87+ if (processingTime > SAFE_PROCESSING_TIME ) {
88+ logger .warn ("Processing time for buffered append-output is high: " +
89+ processingTime + " milliseconds." );
90+ } else {
91+ logger .debug ("Processing time for append-output took "
92+ + processingTime + " milliseconds" );
93+ }
10594
106- if (sizeProcessed > SAFE_PROCESSING_STRING_SIZE ) {
107- logger .warn ("Processing size for buffered append-output is high: " +
108- sizeProcessed + " characters." );
109- } else {
110- logger .debug ("Processing size for append-output is " +
111- sizeProcessed + " characters" );
112- }
113- try {
114- Thread .sleep (BUFFER_TIME_MS );
115- } catch (InterruptedException e ) {
116- logger .error ("Append output thread interrupted: " + e .getMessage ());
117- break ;
95+ Long sizeProcessed = new Long (0 );
96+ for (String noteId : noteMap .keySet ()) {
97+ for (String paragraphId : noteMap .get (noteId ).keySet ()) {
98+ String data = noteMap .get (noteId ).get (paragraphId ).toString ();
99+ sizeProcessed += data .length ();
100+ listener .onOutputAppend (noteId , paragraphId , data );
118101 }
119102 }
103+
104+ if (sizeProcessed > SAFE_PROCESSING_STRING_SIZE ) {
105+ logger .warn ("Processing size for buffered append-output is high: " +
106+ sizeProcessed + " characters." );
107+ } else {
108+ logger .debug ("Processing size for append-output is " +
109+ sizeProcessed + " characters" );
110+ }
120111 }
121112
122113 public static void appendBuffer (String noteId , String paragraphId , String outputToAppend ) {
0 commit comments