When no attention is paid to OSX, the java thread goes to sleep

I'm writing a program to listen for changes to the system clipboard The listener runs on a separate thread and performs some operations (for example, writing to a file) when the contents of the clipboard change

I'm polling the clipboard using the clipboard owner interface so that when my program loses ownership of the clipboard (meaning another process has modified the clipboard), an event will be triggered in my program to let me read the changes

public class OwnershipClipboardListener extends Thread implements ClipboardOwner
{
    private Clipboard clipB = Toolkit.getDefaultToolkit().getSystemClipboard();

    public void run()
    {
        /* Initialize ClipboardListener and gain ownership of clipboard */
    }

    @Override
    public void lostOwnership(Clipboard clipboard,Transferable transferable)
    {
        /* Auto-fired when I lose Clipboard ownership.
           Can do processing and regaining ownership here */
    }    
}

The problem is that when running in OSX, any changes to the clipboard will be reflected only when I manually move the CMD tab to the running process icon in the dock Therefore, if there are multiple clipboard operations before switching to the dock icon, only the last one has any effect I don't have this problem on Linux or windows

This is like a thread going to sleep when the program loses focus, but the last event trigger will still trigger when it wakes up Is there any way to prevent this kind of sleep?

Solution

I suspect that OSX does not provide notification of clipboard changes, so Java will notify you when it wakes up for other reasons

My suspicions come from the nspasteboard documentation, especially the changecount routine It says, "so you can record the change count when you take ownership of the pasteboard, and then compare it with the value returned from changecount to determine whether you still have ownership." There is no mention of using events to detect changes

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>