java – 为什么Clipboard Listener使用更多的CPU使用率?

我编写了监听剪贴板并正常工作的程序,但我看到该程序使用了更多的CPU使用率. CPU使用率超过70%.为什么?我能减少吗?或者我想要监听窗口复制动作的监听器. java中是否有监听器?
这是我的代码:

    package yoxlama;


    import java.awt.*;  
    import java.awt.datatransfer.*;  
    import java.io.IOException;
    import java.net.MalformedURLException;

    class ClipBoardListener extends Thread implements ClipboardOwner {  
      Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();  
      static ClipBoardListener b;

      Interlsoft interlsoft = new Interlsoft();
      public void run() {  
          try{
            Transferable trans = sysClip.getContents(this);  
            regainOwnership(trans);
          }catch (Exception e) {
                try {
                    b.finalize();
                } catch (Throwable e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                 b= new ClipBoardListener();
                 b.start();
              System.out.println("NESE1");
        }
        System.out.println("Listening to board...");  
        while(true) {}  
      }  

      public void lostOwnership(Clipboard c, Transferable t) {  
          try {  
                Thread.sleep(50);  
              } catch(Exception e) {  
                System.out.println("Exception: " + e);  
              } 
          try{
              Transferable contents = sysClip.getContents(this);  
              processContents(contents);  
              regainOwnership(contents); 
          }catch (Exception e) {
              try {
                  b.finalize();
                 b= new ClipBoardListener();
                 b.start();
            } catch (Throwable e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
              System.out.println("nese");
        }
      }  

      void processContents(Transferable t) throws MalformedURLException, IOException {  
          String str = getClipboard(t);
          if(str!=null){
            str=  str.replace("\n", "%0D%0A");
            str=  str.replace("\r", "");
            str=  str.replace("\t", "");
            str=  str.replace("/", "");
            str=  str.replace("-", "");
              interlsoft.translate(str);

          }
     //   System.out.println("Processing: " + getClipboard(t));  

      }  

      void regainOwnership(Transferable t) {  
        sysClip.setContents(t, this);  
      }  

      public static void main(String[] args) {  
          b = new ClipBoardListener();  
        b.start();  
      }  

      public static String getClipboard(Transferable t) {
          //  Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);

            try {
                if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                    String text = (String)t.getTransferData(DataFlavor.stringFlavor);
                    return text;
                }
            } catch (UnsupportedFlavorException e) {
                System.out.println("BURDA1");
            } catch (IOException e) {
                System.out.println("BURDA1");
            }
            return null;
        }
    }  

解决方法:

这:while(true){}

你的CPU很差.而不是真的,只是使该线程无限期地睡眠.见:How do you hang a thread in Java in one line?

上一篇:关于vue使用clipboard复制,第一次无效第二次以后才可以解决


下一篇:【JS插件】【9】操作剪切板 clipboard.js 与 ZeroClipboard.js