java多线程current thread not owner 错误
刚刚写了个多线程的程序,系统报current thread not owner错误,望各位大虾指点指点
import java.util.Queue;
public class MyExec extends Thread {
private final Queue<Runnable> workQueue ;
private final Object o ;
private Runnable r = null;
public MyExec(Queue<Runnable> queue,Object o) {
this.workQueue = queue;
this.o = o;
}
public void run() {
while (true) {
//从对列中拿取对象,开始执行run方法,队列为空就阻塞
if (workQueue.isEmpty()) {
try {
[color=#FF0000]o.wait();//这个地方报异常
} catch (InterruptedException e) {
e.printStackTrace();
}
}
r = workQueue.remove();
r.run();
}
}
}
客户端类:
import java.util.Queue;
public class Client implements Runnable {
private Queue<Runnable> execQueue = null;
private int i = 0;
private final Object o ;
public Client(Queue<Runnable> queue,Object o,int i)
{
this.execQueue = queue;
this.o =o;
this.i = i;
}
public void start()
{
execQueue.add(this);
o.notify();
}
public void run() {
System.out.println("线程"+i+"开始运行");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("------------线程"+i+"运行结束退出");
}
}
主线程类:
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
public class MainTest {
public static void main(String[] args) {
int execThreadNum = 10;
int clientThreadNum = 200;
final Object o = new Object();
MyExec[] exec = new MyExec[execThreadNum];
Client[] client = new Client[clientThreadNum];
Queue<Runnable> workQueue = new ConcurrentLinkedQueue<Runnable>();
for(int i= 0;i<exec.length;i++)
{
exec[i] = new MyExec(workQueue,o);
exec[i].start();
}
for(int i = 0;i<client.length;i++)
{
client[i] = new Client(workQueue,o,i);
client[i].start();
}
}
}
[/color]