我已经实现了一个类似于文档中的示例的空闲javamail监视器。但是,有时会触发空闲的两个或多个同时线程,这会导致for的folder.idle()不再停止运行。这导致大量网络被消耗,并且它们不会停止。
有什么方法可以检测到已经存在一个imap线程,以便另一个线程无法启动?
以下是您在日志中打印内容的示例
10-21 14:22:11.595 8095-24908/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6764,5,main]
10-21 14:22:11.595 8095-22625/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
10-21 14:22:11.595 8095-22625/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6751,5,main]
10-21 14:22:11.596 8095-24908/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
10-21 14:22:11.596 8095-24908/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6764,5,main]
10-21 14:22:11.596 8095-23671/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
10-21 14:22:11.596 8095-23671/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6755,5,main]
10-21 14:22:11.597 8095-24908/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
10-21 14:22:11.597 8095-24908/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6764,5,main]
10-21 14:22:11.597 8095-22625/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
10-21 14:22:11.598 8095-22625/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6751,5,main]
10-21 14:22:11.598 8095-23671/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
10-21 14:22:11.598 8095-23671/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6755,5,main]
10-21 14:22:11.599 8095-22625/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
10-21 14:22:11.599 8095-22625/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6751,5,main]
10-21 14:22:11.599 8095-22625/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
10-21 14:22:11.599 8095-22625/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6751,5,main]
10-21 14:22:11.600 8095-22625/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
10-21 14:22:11.600 8095-22625/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6751,5,main]
10-21 14:22:11.600 8095-22625/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
10-21 14:22:11.600 8095-22625/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6751,5,main]
10-21 14:22:11.602 8095-22625/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
10-21 14:22:11.606 8095-22625/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6751,5,main]
10-21 14:22:11.607 8095-24908/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
10-21 14:22:11.607 8095-24908/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6764,5,main]
10-21 14:22:11.607 8095-23671/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
10-21 14:22:11.607 8095-23671/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6755,5,main]
10-21 14:22:11.608 8095-24908/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
10-21 14:22:11.608 8095-24908/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6764,5,main]
10-21 14:22:11.608 8095-22625/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
10-21 14:22:11.608 8095-22625/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6751,5,main]
10-21 14:22:11.609 8095-23671/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
10-21 14:22:11.609 8095-23671/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6755,5,main]
10-21 14:22:11.609 8095-23671/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
10-21 14:22:11.609 8095-23671/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6755,5,main]
10-21 14:22:11.610 8095-24908/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
10-21 14:22:11.610 8095-24908/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6764,5,main]
10-21 14:22:11.610 8095-22625/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
10-21 14:22:11.610 8095-22625/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6751,5,main]
10-21 14:22:11.610 8095-23671/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
10-21 14:22:11.610 8095-23671/X I/System.out: DEBUG IMAP: waitIfIdle: wait to be not idle: Thread[Thread-6755,5,main]
10-21 14:22:11.611 8095-24908/X I/System.out: DEBUG IMAP: waitIfIdle: idleState 2
我只订阅了一个文件夹收件箱。我真的不明白为什么要创建几个线程......
这是代码......
props = getServerProperties(account.getEmail(), false);
session = null;
store = null;
session = Session.getDefaultInstance(props, null);
store = session.getStore("imap");
if(!store.isConnected())
{
store.connect(account.getEmail(), account.getPassword());
}
folderInbox = store.getFolder("inbox");
if(folderInbox == null || !folderInbox.exists())
{
internalError();
return;
}
folderInbox.open(Folder.READ_WRITE);
SearchTerm searchTerm = new AndTerm(unReviewFlagTerm, unDeleteFlagTerm);
Message[] allmessages =folderInbox.search(searchTerm, folderInbox.getMessages());
...
try {
folderInbox.expunge();
} catch (MessagingException e) {
e.printStackTrace();
}
folderInbox.addMessageCountListener(new MessageCountAdapter() {
public void messagesAdded(MessageCountEvent ev) {
Log.d("Monitor new message", lastNewMessage.toString());
Message[] msgs = ev.getMessages();
}
});
int freq = Integer.parseInt("180000"); // 3 minutes
boolean supportsIdle = false;
try {
if (folderInbox instanceof IMAPFolder) {
IMAPFolder f = (IMAPFolder)folderInbox;
f.idle();
supportsIdle = true;
}
} catch (FolderClosedException fex) {
internalError();
supportsIdle = false;
throw fex;
}catch (MessagingException mex) {
supportsIdle = false;
}
for (;;) {
if (supportsIdle && folderInbox instanceof IMAPFolder) {
IMAPFolder f = (IMAPFolder)folderInbox;
f.idle();
}else{
Thread.sleep(freq);
folderInbox.getMessageCount();
}
}