当前位置:首页 > 开发 > 开源软件 > 正文

netbeans 多线程 IllegalStateException

发表于: 2012-05-25   作者:cobble19   来源:转载   浏览:
摘要: WARNING [org.netbeans.core.windows.WindowManagerImpl] java.lang.IllegalStateException: Problem in some module which uses Window System: Window System API is required to be called from AWT thread only,
WARNING [org.netbeans.core.windows.WindowManagerImpl]
java.lang.IllegalStateException: Problem in some module which uses Window System: Window System API is required to be called from AWT thread only, see http://core.netbeans.org/proposals/threading/


简单说明:
此问题出现的原因大概是:Window System API 只能被AWT 线程(主线程)调用。也可以简单理解为,在别的线程里面不能对netbeans的控件进行操作,比如给控件赋值等。

1.其他线程怎么操作【netbeans的控件】?
2.用设计模式中的一种【观察者模式】。线程只负责传值。观察者负责处理问题。
3.例子代码。( 代码不能直接使用)

4.ObserverBridgeRunnable.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
imports..

/**
 * 自定义线程
 * @author gby
 */
public class ObserverBridgeRunnable implements Runnable {

    private DataObservable dataObservable;
    
    public ObserverBridgeRunnable(DataObservable DataObservable) {
        this.DataObservable = DataObservable;
    }

    @Override
    public void run() {
        HandledData handledData = new HandledData();
        DataObservable.handleTcpData(handledData);
    }

}


5.DataObservable.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

imports..

/**
 * 被观察者
 * Tcp通讯被监视者
 * @author gby
 */
public class DataObservable  extends Observable {
    private static DataObservable instance = null;
    private DataObservable() {
    }
    
    public static synchronized DataObservable getInstance() {
        if (instance == null) {
            instance = new DataObservable();
        }
        return instance;
    }
    
    public static DataObservable newInstance() {
        return new DataObservable();
    }
    
    public void handleTcpData(Object data) {
        super.setChanged();
        notifyObservers(data);
    }
}


6.DataObserver.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
imports..

/**
 * 观察者
 * @author gby
 */
public class DataObserver implements Observer {

    @Override
    public void update(Observable o, final Object arg) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                HandledData handledData = (HandledData) arg;
                // 处理数据
                Manage.hanled(handledData);
            }
        });
    }
}


【4,5,6】为处理所需的类,【7】为调用方法。
7.调用方法
// 建立观察者模式
DataObservable DataObservable = DataObservable.newInstance();
DataObservable.addObserver(new DataObserver());
// 处理数据
ObserverBridgeRunnable observerBridgeRunnable = new ObserverBridgeRunnable(inputStreamReader, DataObservable);
Thread thread = new Thread(observerBridgeRunnable);
thread.start();

引用
Cobble.Ge 于 合肥 公司 20120525

netbeans 多线程 IllegalStateException

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号