My sample code posted below shows two classes. One implements KeyListener and the other implements Runnable and is running in an infinite loop sleeping every 20 ms. When a k
Your charArray
variable is accessed from at least two threads (the one you start in Process and the EDT in your Input class) so you need to synchronize those accesses to ensure visibility (i.e. make sure that changes made by one thread are visible from the other thread).
Note that there are several other issues in your code, for example:
input = new Input(this)
or component.addKeyListener(this)
) - this can lead to weird behaviour in a multi-threaded environmentJFrame
variable inside your Process
class instead of extending JFrame
running
to false, but there is no synchronization around that variable in your run
method so you might not see it become false.