클립 보드를 듣고 올바르게 작동하는 프로그램을 작성했지만 프로그램에서 더 많은 CPU 사용을 보았습니다. CPU 사용률이 70 % 이상입니다. 왜? 그것을 줄일 수 있을까요? 또는 Listener가 Windows의 복사 작업을 듣기를 원합니다. 이것에 대한 자바에 리스너가 있습니까?왜 클립 보드 수신기가 더 많은 CPU 사용을 사용합니까?
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) {}'? –
질문과 관련이 없지만 여전히 : b.finalize() = no, no, no! 재정의 된 것처럼 보이지 않더라도 (대부분의 경우가 아니어야 함) 명시 적으로 호출하는 방법이 아닙니다. –
"while (true) {}"이있는 질문에 +1 하시겠습니까? – Siddharth