[snow-cvs] r74 - trunk/src/java/snow/swing

Alessio Stalla astalla at common-lisp.net
Wed Apr 14 21:40:38 UTC 2010


Author: astalla
Date: Wed Apr 14 17:40:38 2010
New Revision: 74

Log:
Possibly improved ConsoleDocument wrt. thread safety. Still there seems to be a deadlock when typing at high speed while another thread is writing on the console.


Modified:
   trunk/src/java/snow/swing/ConsoleDocument.java

Modified: trunk/src/java/snow/swing/ConsoleDocument.java
==============================================================================
--- trunk/src/java/snow/swing/ConsoleDocument.java	(original)
+++ trunk/src/java/snow/swing/ConsoleDocument.java	Wed Apr 14 17:40:38 2010
@@ -45,6 +45,7 @@
 import javax.swing.JFrame;
 import javax.swing.JScrollPane;
 import javax.swing.JTextArea;
+import javax.swing.SwingUtilities;
 import javax.swing.event.DocumentEvent;
 import javax.swing.event.DocumentListener;
 import javax.swing.text.AttributeSet;
@@ -86,31 +87,39 @@
 		}
 	};
 	
-	private Writer writer = new Writer() {
-
-		@Override
-		public void close() throws IOException {}
+    private Writer writer = new Writer() {
+	    
+	    @Override
+	    public void close() throws IOException {}
 
-		@Override
-		public void flush() throws IOException {}
+	    @Override
+	    public void flush() throws IOException {}
 
-		@Override
-		public void write(char[] cbuf, int off, int len) throws IOException {
-			synchronized(reader) {
-				try {
-					if(inputBuffer.toString().trim().isEmpty()) {
-						int length = inputBuffer.length();
-						inputBuffer.delete(0, length);
-						lastEditableOffset -= length;
-					}
-					reader.notifyAll();
+	    @Override
+	    public void write(final char[] cbuf, final int off, final int len) throws IOException {
+		synchronized(reader) {
+		    try {
+			if(inputBuffer.toString().trim().isEmpty()) {
+			    int length = inputBuffer.length();
+			    inputBuffer.delete(0, length);
+			    lastEditableOffset -= length;
+			}
+			SwingUtilities.invokeAndWait(new Runnable() {
+				public void run() {
+				    try {
 					superInsertString(getLength(), new String(cbuf, off, len), null);
-					lastEditableOffset = getLength();
-				}  catch (Exception e) {
-					throw new RuntimeException(e);
+				    } catch(Exception e) {
+					assert(false); //BadLocationException should not happen here
+				    }
 				}
-			}
+			    });
+			lastEditableOffset = getLength();
+			reader.notifyAll();
+		    }  catch (Exception e) {
+			throw new RuntimeException(e);
+		    }
 		}
+	    }
 	};
 	
 	private boolean disposed = false;




More information about the snow-cvs mailing list