I have such code in my application which I want to run in background:
import java.awt.*;
import java.awt.datatransfer.*;
class Main extends Thread implements ClipboardOwner {
private Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
public void run() {
Transferable trans = sysClip.getContents(this);
regainOwnership(trans);
System.out.println("Listening to board...");
while (true) { }
}
public static void main(String[] args) {
Main b = new Main();
b.start();
}
//............
It listens for Ctrl+C
and prints the content of clipboard once this key combination is pressed. However, it makes CPU run 99% all the time. If I remove while (true) { }
then the application will just stop in a second.
I tried to launch it as java -jar myapp.jar &
but the result was the same. And it would be the same on Windows I guess, although I care about Linux for now.
What do I do about that?