我目前正在使用 OSGi
开发一个数据管理器,该数据管理器定期从某些传感器获取数据。我获取数据的方式如下:
public void run() {
while (!stop || !Thread.currentThread().isInterrupted()) {
try {
List<DataEntry> aux;
long millis = System.currentTimeMillis();
for (DataLogger dl : loggers) {
String name = dl.getDriverName();
aux = dataTable.get(name);
if (aux == null) {
aux = new ArrayList<DataEntry>();
}
dl.readValue();
DataEntry de = new DataEntry(dl.getCurrentValue(), millis);
aux.add(de);
dataTable.put(dl.getDriverName(), (ArrayList<DataEntry>) aux);
}
Thread.sleep(PERIOD);
// ***** Exceptions *****
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我面临的问题是,当线程
正在运行时,我无法与控制台
交互,这意味着我无法阻止包的执行控制台并与执行交互。
有没有办法让bundle
在与主线程不同的线程中运行?
*** 更新: 我直接调用 run 方法,而不是启动线程,这就是问题所在。
最佳答案
您忽略了代码中最重要的部分,即 run() 方法实际执行的方式。事实上,它被称为 run() 表明该方法位于实现 Runnable 的类中,这反过来表明您实际上正在启动一个新的 Thread() 来执行它。但是,我从您的问题猜测,您实际上并没有这样做,而是直接从启动 bundle 的线程运行该方法(从 BundleActivator 的 start() 方法调用它)。如果确实如此,则不会启动其他 bundle ,并且该 bundle 将保持在 STARTING 状态。要解决这个问题,请生成一个新线程。
但是我可能会离开,请发布您其余的代码! :)
关于java - 在线程运行时使用 OSGi 控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31798370/