[英]What Is The Correct Way To Continuously Loop An Application
My question isn't specific to Java but that is the language I'm using to achieve what I want. 我的问题并非特定于Java,而是我用来实现自己想要的语言。
I'm experimenting with Bluetooth in Java and have written a simple terminal program ie no GUI interface that searches for nearby Bluetooth devices and lists them. 我正在用Java进行蓝牙实验,并编写了一个简单的终端程序,即没有GUI界面可以搜索附近的蓝牙设备并列出它们。 My code is as follows:
我的代码如下:
import javax.bluetooth.*;
public class BluetoothTest implements DiscoveryListener{
private static boolean isAlive = true;
public static void main(String[] args) {
try {
LocalDevice ld = LocalDevice.getLocalDevice();
if (LocalDevice.isPowerOn()){
System.out.println("Power On.");
System.out.println("Friendly Name: " + ld.getFriendlyName());
System.out.println("Address: " + ld.getBluetoothAddress());
DiscoveryAgent da = ld.getDiscoveryAgent();
da.startInquiry(DiscoveryAgent.GIAC,new BluetoothTest());
while (isAlive){
/* Sleep */
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
System.out.println("Power Off.");
}
} catch (BluetoothStateException e) {
System.out.println(e.toString());
}
}
public void setAlive(boolean status){
isAlive = status;
}
public void deviceDiscovered(RemoteDevice rd, DeviceClass dc){
try{
System.out.println(rd.getFriendlyName(true));
} catch (java.io.IOException e){
System.out.println(e.toString());
}
}
public void inquiryCompleted(int discType){
isAlive = false;
}
public void servicesDiscovered(int transID, ServiceRecord[] sr){
}
public void serviceSearchCompleted(int transID, int respCode){
}
}
The startInquiry() of DiscoveryAgent object returns immediately and any devices that are discovered are returned to the DiscoveryListener interface which I have implemented. DiscoveryAgent对象的startInquiry()立即返回,发现的所有设备都返回到我已实现的DiscoveryListener接口。 The problem is unless I include the while() loop the program will terminate before any devices are discovered.
问题是除非我包括while()循环,否则程序将在发现任何设备之前终止。
Just how do applications efficiently stay resident? 应用程序如何有效地保持驻留状态? Is it achieved by having a separate 'working' thread and a main thread which spawns the working thread but itself sleeps until the worker had finished?
它是通过拥有一个单独的“工作”线程和一个主线程来实现的,而该主线程会产生该工作线程,但其自身会一直休眠直到工人完成工作为止?
You may use Object wait / notify mechanism to hold main method until BluetoothTest notifies on a common object lock. 您可以使用对象等待/通知机制来保存主要方法,直到BluetoothTest通知通用对象锁为止。
Just a pseudo code, 只是一个伪代码,
Define a static final Object _mutex = new Object(); 定义一个静态最终对象_mutex = new Object();
In main method after call of startInquiry call _mutex.wait(); 在main方法中,调用startInquiry之后,调用_mutex.wait();。 This will hold the main thread.
这将保留主线程。
In inquiryCompleted call _mutex.notify(); 在queryCompleted中调用_mutex.notify(); This will release the main thread.
这将释放主线程。
Please note that this code will work only if startInquiry create a new thread and invokes call back methods. 请注意,此代码仅在startInquiry创建新线程并调用回调方法时才有效。 I am not much aware of DiscoverAgent class.
我不太了解DiscoverAgent类。 So if that's not the case, the above solution might now work.
因此,如果不是这种情况,上述解决方案现在可能会起作用。
Java programs will stay running until all threads that are not marked as "daemon" exit. Java程序将一直运行,直到所有未标记为“守护程序”的线程退出为止。 If the
Main
thread exits and there are no other threads running then the program will quit. 如果
Main
线程退出并且没有其他线程在运行,则程序将退出。 If you have spawned another thread (or if DiscoveryAgent
is running in another thread) that is not a "daemon" thread, then Java will keep running until the thread exits. 如果生成了不是“守护程序”线程的另一个线程(或者
DiscoveryAgent
在另一个线程中运行),则Java将一直运行直到该线程退出。 Putting the loop in main is a fine way of doing it although as @jatanp mentioned, using wait
/ notify
is cleaner. 将循环放入main是一种不错的方法,尽管正如@jatanp所述,使用
wait
/ notify
更为干净。
Couple of things about your code: 有关代码的几件事:
It is strange to have your Main code also be your DiscoveryListener
. 让您的Main代码也成为
DiscoveryListener
是很奇怪的。 I would isolate that functionality in another class: 我将在另一个类中隔离该功能:
public class BluetoothTest { ... private static class OurListener implements DiscoveryListener { } }
isAlive
, it should be marked as volatile
or you need to synchronize
around it. isAlive
,所以应将其标记为volatile
否则您需要围绕它进行synchronize
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.