I need to change dynamically the code in the listing by adding just one key word to the code. I tried to do it like this:
\begin{lstlisting}[escapechar=|]
class MyThread extends Thread {
private |\only<3>{\alert{volatile}}| boolean stop = false;
public void run() {
while (!stop) {
doSomeWork();
}
}
public void setStop() {
this.stop = true;
}
}
\end{lstlisting}
It works ok, but there are two spaces between "private" and "boolean" when the "volatile" is not visible. Is there any other way to make what I want?

|seems to do. – egreg Feb 1 at 11:52