【发布时间】:2016-08-30 22:01:29
【问题描述】:
这是我的线程代码
Thread decode = new Thread(new Runnable() {
@Override
public void run() {
// System.out.println("decode thread started");
synchronized (queueLock) {
while (true) {
if (!queue.isEmpty()) {
System.out.println(decoding());
}
}
}
}
});
这是我的会员字段
private PriorityQueue<String> queue = new PriorityQueue<String>();
private Object queueLock = new Object();
这是我的方法
public Character decoding() {
String code = queue.poll();
Character chr;
Character[][] key = kG.getKey();
String binary = null;
Character startingCharacter = code.charAt(0);
int iK = 0;
int ascii;
//System.out.println(code); when i use this other threads do not start also
for (int i = 0; i < KeyGenerator.getIr(); i++)
if (startingCharacter == key[i][0])
iK = i;
for (int j = 0; j < KeyGenerator.getJr(); j++) {
if (code.contains(String.valueOf(key[iK][j])))
binary = binary + "1";
else
binary = binary + "0";
}
ascii = Integer.parseInt(binary, 2);
chr = (char) (ascii + 'a');
return chr;
}
prioritqueue 正在被另一个线程使用,但我使用queueLock object 使其同步。
decoding 的方法在 docode thread 中不起作用!!!
【问题讨论】:
-
你排的怎么样?
-
为什么同步块中有无限循环?你希望那把锁永远解锁吗?
-
@Lee 我正在用另一个线程填充它
-
@JonnyHenly 我应该使用 notify() 来解锁吗?
标签: java multithreading methods locking synchronized