У меня есть графический интерфейс с таблицей - эта таблица заполняется содержимым InputStreamReader.Для этого я создаю поток, который отслеживает InputStream, и когда появляется новая строка для чтения, он добавляет ее в таблицу.Проблема, с которой я сталкиваюсь, заключается в том, что создание потока, похоже, приводит к зависанию всего приложения.Какой типичный способ сделать это?
Поток идет:
Gui-> Button onUp-> LogCatController.start-> gui.getDisplay (). AsyncExec (AdbThreadReader) -> addLine
public class Gui {
protected Shell shell;
private Display display;
private Table logCatTable;
private Text text;
private LogCatController logCatController;
private TableColumn tblclmnDate;
public void open() {
this.display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
...
logCatTable = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
FormData fd_logCatTable = new FormData();
fd_logCatTable.left = new FormAttachment(0, 142);
fd_logCatTable.right = new FormAttachment(100);
fd_logCatTable.bottom = new FormAttachment(100);
fd_logCatTable.top = new FormAttachment(0);
logCatTable.setLayoutData(fd_logCatTable);
logCatTable.setHeaderVisible(true);
logCatTable.setLinesVisible(true);
...
Button btnStart = new Button(composite_1, SWT.NONE);
btnStart.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent arg0) {
try {
logCatController.start();
} catch (IOException e) {
e.printStackTrace();
}
}
});
...
}
public Table getLogCatTable() {
return logCatTable;
}
public Display getDisplay() {
return display;
}
}
public class LogCatController {
private DataBindingContext m_bindingContext;
private static Logger logger = Logger.getLogger(AdbLine.class);
private LogCat logcat;
private Gui gui;
public LogCatController(Gui gui){
this.gui = gui;
logcat = new LogCat();
m_bindingContext = initDataBindings();
}
public void start() throws IOException{
logcat.execute();
BufferedReader read = logcat.getSTDOUT();
BufferedReader error = logcat.getSTDERR();
Runnable readRunnable = new AdbLineReaderThread(read);
gui.getDisplay().asyncExec(readRunnable);
}
private void addLine(AdbLine l){
logger.debug("Adding: " + l);
Table logCatTable = this.gui.getLogCatTable();
TableItem tableItem = new TableItem(logCatTable, SWT.NONE);
tableItem.setText(new String[] {"time", l.getLevel().toString(), String.valueOf(l.getPid()), l.getMessage()});
tableItem.setForeground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
}
public class AdbLineReaderThread implements Runnable {
private BufferedReader read;
public AdbLineReaderThread(BufferedReader read){
this.read = read;
}
public void run() {
logger.debug("AdbLinReaderThread run");
String line = null;
try{
while( (line = read.readLine()) != null){
logger.debug(line);
AdbLine l = new AdbLine(line);
addLine(l);
}
} catch(Exception e) {
System.out.println("Could not parse: " + line);
e.printStackTrace();
}
}
}
}
public class LogCat extends BackgroundCommand{
public LogCat(){
super(...);
}
}