1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-18 06:25:23 +02:00

[217429][api] Make registering multiple output listeners thread-safe

This commit is contained in:
Martin Oberhuber 2008-02-01 13:03:19 +00:00
parent 78e7d6b002
commit ab54db66b6

View file

@ -13,11 +13,13 @@
* *
* Contributors: * Contributors:
* Martin Oberhuber (Wind River) - [197848] Fix shell terminated state when remote dies * Martin Oberhuber (Wind River) - [197848] Fix shell terminated state when remote dies
* Martin Oberhuber (Wind River) - [217429] Make registering multiple output listeners thread-safe
*******************************************************************************/ *******************************************************************************/
package org.eclipse.rse.services.shells; package org.eclipse.rse.services.shells;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
public abstract class AbstractHostShellOutputReader extends Thread implements IHostShellOutputReader public abstract class AbstractHostShellOutputReader extends Thread implements IHostShellOutputReader
@ -38,7 +40,7 @@ public abstract class AbstractHostShellOutputReader extends Thread implements I
public AbstractHostShellOutputReader(IHostShell hostShell, boolean isErrorReader) public AbstractHostShellOutputReader(IHostShell hostShell, boolean isErrorReader)
{ {
_hostShell = hostShell; _hostShell = hostShell;
_listeners = new ArrayList(); _listeners = Collections.synchronizedList(new ArrayList());
_linesOfOutput = new ArrayList(); _linesOfOutput = new ArrayList();
_consumerOffset = 0; _consumerOffset = 0;
_isErrorReader = isErrorReader; _isErrorReader = isErrorReader;
@ -102,12 +104,18 @@ public abstract class AbstractHostShellOutputReader extends Thread implements I
} }
} }
protected final synchronized void startIfNotAlive() {
if (!isAlive()) {
start();
}
}
public IHostOutput readLine() public IHostOutput readLine()
{ {
if (!isAlive()) if (!isAlive())
{ {
internalReadLine(); internalReadLine();
start(); startIfNotAlive();
} }
return (IHostOutput)_linesOfOutput.get(_consumerOffset++); return (IHostOutput)_linesOfOutput.get(_consumerOffset++);
} }
@ -126,10 +134,7 @@ public abstract class AbstractHostShellOutputReader extends Thread implements I
public void addOutputListener(IHostShellOutputListener listener) public void addOutputListener(IHostShellOutputListener listener)
{ {
_listeners.add(listener); _listeners.add(listener);
if (!isAlive()) startIfNotAlive();
{
start();
}
} }
public void fireOutputChanged(IHostShellChangeEvent event) public void fireOutputChanged(IHostShellChangeEvent event)