我正在尝试编写一个简单的消息管理器,但我的队列中没有显示任何消息。
调用它的代码是:
//initialize message manager
CJournal.Journal(Main.class, "Initializing message manager");
MessageManager messageManager = new MessageManager();
messageManager.run();
//send bootstrap message
CJournal.Journal(Main.class, "Testing messanger: ending bootstrapping message to messagemanager");
Message boot_strap_message = new Message();
boot_strap_message.mflag = EEventMachine.EM_BOOTSTRAP;
messageManager.EnqueueMessage(boot_strap_message);
消息管理器代码是:
public class MessageManager implements Runnable {
PriorityQueue<Message> messagesQueue = new PriorityQueue<Message>();
public void EnqueueMessage(Message message)
{
messagesQueue.add(message);
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true)
{
//pull message from queue
if (!messagesQueue.isEmpty())
{
CJournal.Journal(MessageManager.class, "Found a message");
Message message = messagesQueue.remove();
//dispatch messages according to their type
switch(message.mflag)
{
case EM_BOOTSTRAP:
CJournal.Journal(MessageManager.class, "Messaging system working properly.");
default:
CJournal.Journal(MessageManager.class, "Dispatch Message Trashed");
}
}
}
}
调试输出为:
13 In Class::class Sampler.Main Journal Entry:Testing Event Manager
14 In Class::class Sampler.Main Journal Entry:Created dummy CEvent. Pushing to event manager.
15 In Class::class Sampler.Main Journal Entry:Initializing message manager
我应该得到,&#34;消息系统正常工作&#34;
有什么想法吗?
--------------------------------------------------------------
O`11 In Class::class SchedulingManager.Timer.CTimerManager Journal Entry:Creating a CTimer Object
12 In Class::class SchedulingManager.Timer.CTimerManager Journal Entry:Timer creation success. Adding CTimer object to internal list Of Timers
13 In Class::class Sampler.Main Journal Entry:Testing Event Manager
14 In Class::class Sampler.Main Journal Entry:Created dummy CEvent. Pushing to event manager.
15 In Class::class Sampler.Main Journal Entry:Initializing message manager
16 In Class::class Sampler.Main Journal Entry:Testing messanger: ending bootstrapping message to messagemanager
Exception in thread "main" Exception in thread "Thread-4" java.lang.ClassCastException: MessageManagement.Message cannot be cast to java.lang.Comparable
at java.util.concurrent.PriorityBlockingQueue.siftUpComparable(Unknown Source)
at java.util.concurrent.PriorityBlockingQueue.offer(Unknown Source)
at java.util.concurrent.PriorityBlockingQueue.add(Unknown Source)
at MessageManagement.MessageManager.EnqueueMessage(MessageManager.java:69)
at Sampler.Main.main(Main.java:128)
java.util.NoSuchElementException
at java.util.AbstractQueue.remove(Unknown Source)
at MessageManagement.MessageManager.run(MessageManager.java:47)
这段代码:
`public class MessageManager extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
while (true)
{
if (!messagesQueue.isEmpty())
{
//pull message from queue
Message message = messagesQueue.remove();
//dispatch messages according to their type
switch(message.mflag)
{
case EM_BOOTSTRAP:
CJournal.Journal(MessageManager.class, "Messaging system working properly.");
default:
CJournal.Journal(MessageManager.class, "Dispatch Message Trashed");
}
}
}
}
public static PriorityBlockingQueue<Message> messagesQueue = new PriorityBlockingQueue<Message>();
public static void EnqueueMessage(Message message)
{
messagesQueue.add(message);
}
这就是我宣布我的主题的方式:
MessageManager messageManager = new MessageManager();
messageManager.start();
//send bootstrap message
CJournal.Journal(Main.class, "Testing messanger: ending bootstrapping message to messagemanager");
Message boot_strap_message = new Message();
boot_strap_message.mflag = EEventMachine.EM_BOOTSTRAP;
messageManager.EnqueueMessage(boot_strap_message);
Loop loop = new Loop(); //hand over to game loop
我不确定问题是什么。
答案 0 :(得分:0)
您正在同一run()
内MessageManager
呼叫Thread
,然后进入无限循环。
您需要将MessageManager
包装在新的Thread
中并启动该帖子。
new Thread(messageManager).start();
但是,如果您要从多个线程访问它,则可能需要更改PriorityQueue
来自JavaDoc:
请注意,此实现未同步。如果任何线程修改队列,则多个线程不应同时访问PriorityQueue实例。而是使用线程安全的PriorityBlockingQueue类。