使用jcheckbox停止线程/对象的工作

时间:2014-10-18 21:47:26

标签: java multithreading

我有线程类

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;

public class BoardListener extends Thread implements ClipboardOwner {


    Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();

    @Override
    public void run() {
        Transferable trans = sysClip.getContents(this);
        regainOwnership(trans);
        System.out.println("Listening to board...");
        while(!isInterrupted()){
        }
    }

    @Override
    public void lostOwnership(Clipboard c, Transferable t) {
        try {
            this.sleep(50);
        } catch (Exception e) {
            System.out.println("Exception: " + e);
        }
        Transferable contents = sysClip.getContents(this);
        processContents(contents);
        regainOwnership(contents);
    }

    void processContents(Transferable t) {
        try {
            System.out.println("Processing: " + (String)t.getTransferData(DataFlavor.stringFlavor));
        } catch (Exception ex) {
            ex.printStackTrace();
        } 
    }

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

在main()中我创建了object和object.start()

BoardListener b = new BoardListener(); 
b.start();

但我需要以某种方式使用jcheckbox停止/开始工作此对象(停止剪贴板监视)。 尝试了b.interupt();不起作用,尝试在循环运行时设置标志,没有帮助。

0 个答案:

没有答案