Problem mit System.setOut()/setErr() und MultiThreading

Mnemo

Grünschnabel
Habe eine neue Konponente geschrieben mit der ich alles von Sytem.ERR und System.OUT loggen möchte.
es funktioniert zwar nur leider bleibt die CPU-Zeit bei >97% wenn erstmalig in die JTextPane geloogt wird.
Es muss an den PipedStreams liegen nur leider weiß ich nicht wo. Bin wohl betriebsblind.

Kann sich vileicht einer mal meine LoggerRunnable ansehen oder die Komponente mal testen ob das Problem auch auftritt, dass der Thread nach dem ersten Loggen auf >97% CPU-Zeit bleibt.

Danke im Vorraus

Code:
import java.awt.Color;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class Konsole extends JScrollPane {

   private static final long serialVersionUID = 3664915929116677102L;
   
   private final String fehler ="Ausnahmefehler in der Konsole: ";
   
   private JTextPane textfeld;
   private StyledDocument dokument;
   private SimpleAttributeSet out;
        private SimpleAttributeSet err;
   
   private PrintStream streamout;
   private PipedInputStream pipedin;
   private PipedOutputStream pipedout;
   
   public Konsole() {
      super();
      setVisible(true);
      textfeld = new JTextPane();
      textfeld.setEditable(false);
      getViewport().add(textfeld);
      dokument = (StyledDocument) textfeld.getDocument();
      err = new SimpleAttributeSet();
      StyleConstants.setForeground(err, Color.red);
      StyleConstants.setBold(err, true);
      out = new SimpleAttributeSet();
      StyleConstants.setForeground(out, Color.black);
      StyleConstants.setBold(out, true);
      
      try {
         pipedin = new PipedInputStream();
         pipedout = new PipedOutputStream(pipedin);
         streamout = new PrintStream(pipedout);
         System.setOut(streamout);
         Thread thread =new Thread(new LoggerRunnable(pipedin,this));
         thread.start();
      } catch (IOException e) {
         e.printStackTrace();
      }

   }

   public void printErr(String text){
      schreibeMitAttribut(text, err);
   }
   
   public void printOut(String text){
      schreibeMitAttribut(text, out);
   }
   
   public void printlnErr(String text){
      schreibeMitAttribut(text + "\n" ,err);
   }
   
   public void printlnOut(String text){
      schreibeMitAttribut(text + "\n" ,out);
   }
   
   public void schreibeMitAttribut(String text, SimpleAttributeSet set){
      try {
         dokument.insertString(dokument.getLength(), text , set);
         new ScrollingThread(this).start();
      } catch (BadLocationException e) {
         textfeld.setText(textfeld.getText() + fehler + e.getMessage());
      }
   }
   
}

class ScrollingThread extends Thread {

   private Konsole konsole;
   
   public ScrollingThread(Konsole konsole) {
      this.konsole=konsole;
   }

   public void run(){
      synchronized(this){
         konsole.getVerticalScrollBar().setValue(konsole.getVerticalScrollBar().getMaximum());
      }
     }
}

import java.io.IOException;
import java.io.PipedInputStream;;

public class LoggerRunnable implements Runnable {

   private byte[] buffer = new byte[1024];
   private PipedInputStream pipedin;
   private Konsole konsole;
   
   public LoggerRunnable(PipedInputStream pipedin, Konsole konsole) {
      this.pipedin=pipedin;
      this.konsole=konsole;
   }

   public void run() {
      int len;
      while(true){
         try {
            len = pipedin.read(buffer);
            if(len==-1){
               //? Vieleicht hier der Fehler
               break;
            }
            konsole.printlnOut(new String(buffer,0,len));
         } catch (IOException e) {
            //Fehler
         }
      }
   }

}
 
Hallo!

Folgende Variante wäre auch denkbar:
Java:
/**
 * 
 */
package de.tutorials;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.OutputStream;
import java.io.PrintStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

/**
 * @author Tom
 * 
 */
public class SystemStreamRedirectionExample extends JFrame {

	static PrintStream STD_OUT = System.out;

	static PrintStream STD_ERR = System.err;

	public SystemStreamRedirectionExample() {
		super("SystemStreamRedirectionExample");
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		JTextPane textPane = new JTextPane();
		JScrollPane scrollPane = new JScrollPane(textPane);
		scrollPane.setPreferredSize(new Dimension(640, 480));
		add(scrollPane, BorderLayout.CENTER);

		RedirectablePrintStream redirectedErrStream = new RedirectablePrintStream(
				STD_ERR, textPane, createErrAttributeSet());
		redirectedErrStream.setRedirect(true);

		System.setErr(redirectedErrStream);

		RedirectablePrintStream redirectedOutStream = new RedirectablePrintStream(
				STD_OUT, textPane, createOutAttributeSet());
		redirectedOutStream.setRedirect(false);

		System.setOut(redirectedOutStream);

		JButton btnErr = new JButton("Error");
		btnErr.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Exception().printStackTrace();
			}
		});

		JButton btnOut = new JButton("Out");
		btnOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("tutorials.de");
			}
		});

		JPanel panel = new JPanel(new GridLayout(1, 2));
		panel.add(btnErr);
		panel.add(btnOut);
		add(panel, BorderLayout.SOUTH);

		pack();
		setVisible(true);
	}

	private SimpleAttributeSet createOutAttributeSet() {
		SimpleAttributeSet outAttributes = new SimpleAttributeSet();
		StyleConstants.setForeground(outAttributes, Color.black);
		StyleConstants.setBold(outAttributes, true);
		return outAttributes;
	}

	private SimpleAttributeSet createErrAttributeSet() {
		SimpleAttributeSet errAttributes = new SimpleAttributeSet();
		StyleConstants.setForeground(errAttributes, Color.red);
		StyleConstants.setBold(errAttributes, true);
		return errAttributes;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new SystemStreamRedirectionExample();
	}

	static class RedirectablePrintStream extends PrintStream {
		JTextPane textPane;

		boolean redirect;

		SimpleAttributeSet attributeSet;

		public RedirectablePrintStream(OutputStream out, JTextPane textPane,
				SimpleAttributeSet attributeSet) {
			super(out);
			this.textPane = textPane;
			this.attributeSet = attributeSet;
		}

		public void write(byte[] buf, int off, int len) {
			if (redirect) {
				try {
					int startOffSet = textPane.getDocument().getLength();
					textPane.getDocument().insertString(startOffSet,
							new String(buf, off, len), attributeSet);
					textPane.setCaretPosition(startOffSet + len);
				} catch (BadLocationException e) {
					e.printStackTrace();
				}
			} else {
				super.write(buf, off, len);
			}
		}

		public boolean isRedirect() {
			return redirect;
		}

		public void setRedirect(boolean redirect) {
			this.redirect = redirect;
		}
	}
}

Gruß Tom
 
Zurück