如何调用两个主方法来操作同一个消息队列?

linshuyi1217 2017-03-15 09:34:31
现在是这样的,我写了一个消息队列的类,里面涵盖了添加元素和删除元素的功能
public class MsgQueue {
// 声明一个消息队列对象
private Vector<HashMap<String, String>> queue = null;

public Vector<HashMap<String, String>> getQueue() {
return queue;
}

public void setQueue(Vector<HashMap<String, String>> queue) {
this.queue = queue;
}

static Myuser32 myuser32 = Myuser32.INSTANCE;
static User32 user32 = User32.INSTANCE;

// 创建消息队列对象
public MsgQueue() {
queue = new Vector<HashMap<String, String>>();
}

// 将用户传入的map加入消息队列列尾
public synchronized void send(HashMap<String, String> map) {
queue.addElement(map);
}

// 将消息队列的元素取出到模拟人工作的方法中
public synchronized HashMap<String, String> recv() {
// 判断如果队列中没有值终止方法
if (queue.size() == 0) {
return null;
}
System.out.println("一开始的大小"+queue.size());
// 将队列中的第一个元素取出
HashMap<String, String> mapping = (HashMap<String, String>) queue
.firstElement();
// 然后将第一个元素删除
// queue.removeElementAt(0);
// 将第一个元素返回
return mapping;
}

// 删除用户需要删除的map
public String delete(HashMap<String, String> map)
throws InterruptedException {
System.out.println("我需要的"+queue.size());
// 遍历消息队列中的map进行比对
for (int i = 1; i < queue.size(); i++) {

HashMap<String, String> hashMap = (HashMap<String, String>) queue
.get(i);
System.out.println("1111111111111111111111111111");
if (map.equals(hashMap)) {
queue.remove(hashMap);
System.out.println("222222222222222222222222222222");
return "删除成功";
} else {
HWND hwndCancel = myuser32.FindWindowEx(
user32.FindWindow(null, "快速转换"), null, "Button", "取消");
myuser32.SendMessage(hwndCancel, 0xF5, 0, null);
while (myuser32.FindWindowEx(
user32.FindWindow(null, "CADdoctor EX6"), null,
"Button", "确定") == null) {
System.out.println("33333333333333333333333333333333");
Thread.sleep(1000);
}
return "取消转换成功";
}
}
return null;
}

// 删除消息队列中的第一个元素
public void remove() {
queue.removeElementAt(0);
}
}

然后写了一个类,类中有添加和删除元素的方法
public class Implement {

Myuser32 myuser32 = Myuser32.INSTANCE;
User32 user32 = User32.INSTANCE;
MsgQueue msgqueue = new MsgQueue();
// 新建模拟操作类和消息队列类
Simulation simulation = new Simulation();

public MsgQueue setElement(UserAndPath userAndPath) {
// 获取用户名和用户传入的绝对路径
String user = userAndPath.getUser();
String route = userAndPath.getPath();
// 新建Map对象将用户名和路径传入map中
HashMap<String, String> map = new HashMap<String, String>();
map.put(user, route);
// 将map放入消息队列
msgqueue.send(map);
System.out.println(msgqueue.getQueue().size());
return msgqueue;
}

public void begin() throws InterruptedException, AWTException,
IOException {
if(msgqueue.getQueue().size()>0){
// 运行模拟操作方法
simulation.runtime(msgqueue);
}
}

public String deleteElement(HashMap<String, String> map) {

Vector<HashMap<String, String>> queue = msgqueue.getQueue();
System.out.println("我需要的" + queue.size());
// 遍历消息队列中的map进行比对
for (int i = 1; i < queue.size(); i++) {

HashMap<String, String> hashMap = (HashMap<String, String>) queue
.get(i);

if (map.equals(hashMap)) {
queue.remove(hashMap);

return "删除成功";
} else {
HWND hwndCancel = myuser32.FindWindowEx(
user32.FindWindow(null, "快速转换"), null, "Button", "取消");
myuser32.SendMessage(hwndCancel, 0xF5, 0, null);
while (myuser32.FindWindowEx(
user32.FindWindow(null, "CADdoctor EX6"), null,
"Button", "确定") == null) {

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("阻塞失败");
}
}
return "取消转换成功";
}
}
return null;
}
}

现在我写一个主方法在消息队列中插入一些元素,如果再写一个主方法删除元素为何删除不了。消息队列的size一直为0是什么原因,能不能有什么方法解决这个问题?
...全文
131 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
linshuyi1217 2017-03-15
  • 打赏
  • 举报
回复
引用 1 楼 zs808 的回复:
因为你用的所有组件都不是线程安全的,在多线程环境下会出现线程安全问题。
那怎么解决这个问题您有什么好办法么?
zs808 2017-03-15
  • 打赏
  • 举报
回复
因为你用的所有组件都不是线程安全的,在多线程环境下会出现线程安全问题。

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧