thread的notify和wait怎么玩?

07-01-30 pushboy
我现在是想在系统起来的时候把所有的thread创建起来

然后有请求过来,就唤醒一个thread来干活

可是唤醒之后,这个thread什么事情都没干

这是怎么回事阿

谢谢

1
pushboy
2007-01-30 15:27
//系统启动时候

for (int i = 0; i < initThreadSize; i++)

{

ServerIThread ServerIthread = new ServerIThread(i);

workThreadsPool.add(ServerIthread);

ServerIthread.start();

synchronized (ServerIthread)

{

try

{

ServerIthread.wait();

}

catch (Exception es)

{

es.printStackTrace();

}

}

}

..............

//有请求过来时候,调用这个方法

synchronized (ServerIthread)

{

ServerIthread.notify();

while (true)

{

boolean isEnd = ServerIthread.isThreadEnd();

if (isEnd)

{

mtMsgMap = ServerIthread.getMtMsgMap();

ServerIthread.wait();

break;

}

}

}

................

//thread的run

public void run()

{

try

{

logger.info("thread " + threadID + " 开始处理...");

isEnd = false;

......

}

}

猜你喜欢