hadoop版本一中的线程唤醒问题
2013-06-09 19:17
519 查看
最近看了下TT的代码,其中关于TaskLauncher是核心。 中间发现有一段代码很有意思:
private synchronized void releaseSlot() { if (slotTaken) { if (launcher != null) { launcher.addFreeSlots(task.getNumSlotsRequired()); } slotTaken = false; } else { // wake up the launcher. it may be waiting to block slots for this task. if (launcher != null) { launcher.notifySlots(); } } }Launcher的notifySlots 函数:
void notifySlots() { synchronized (numFreeSlots) { numFreeSlots.notifyAll(); } }
在TaskLauncher的run()方法中如下:
public void run() { while (!Thread.interrupted()) { try { TaskInProgress tip; Task task; synchronized (tasksToLaunch) { while (tasksToLaunch.isEmpty()) { tasksToLaunch.wait(); } //get the TIP tip = tasksToLaunch.remove(0); task = tip.getTask(); LOG.info("Trying to launch : " + tip.getTask().getTaskID() + " which needs " + task.getNumSlotsRequired() + " slots"); } //wait for free slots to run synchronized (numFreeSlots) { boolean canLaunch = true; while (numFreeSlots.get() < task.getNumSlotsRequired()) { //Make sure that there is no kill task action for this task! //We are not locking tip here, because it would reverse the //locking order! //Also, Lock for the tip is not required here! because : // 1. runState of TaskStatus is volatile // 2. Any notification is not missed because notification is // synchronized on numFreeSlots. So, while we are doing the check, // if the tip is half way through the kill(), we don't miss // notification for the following wait(). if (!tip.canBeLaunched()) { //got killed externally while still in the launcher queue LOG.info("Not blocking slots for " + task.getTaskID() + " as it got killed externally. Task's state is " + tip.getRunState()); canLaunch = false; break; } LOG.info("TaskLauncher : Waiting for " + task.getNumSlotsRequired() + " to launch " + task.getTaskID() + ", currently we have " + numFreeSlots.get() + " free slots"); numFreeSlots.wait(); } if (!canLaunch) { continue; } LOG.info("In TaskLauncher, current free slots : " + numFreeSlots.get()+ " and trying to launch "+tip.getTask().getTaskID() + " which needs " + task.getNumSlotsRequired() + " slots"); numFreeSlots.set(numFreeSlots.get() - task.getNumSlotsRequired()); assert (numFreeSlots.get() >= 0); } synchronized (tip) { //to make sure that there is no kill task action for this if (!tip.canBeLaunched()) { //got killed externally while still in the launcher queue LOG.info("Not launching task " + task.getTaskID() + " as it got" + " killed externally. Task's state is " + tip.getRunState()); addFreeSlots(task.getNumSlotsRequired()); continue; } tip.slotTaken = true; } //got a free slot. launch the task startNewTask(tip); } catch (InterruptedException e) { return; // ALL DONE } catch (Throwable th) { LOG.error("TaskLauncher error " + StringUtils.stringifyException(th)); } } } }
对于notifySlots函数,我的奇怪之处在于, 根据TaskTracker的构造函数而言, 无论是mapLauncher还是reduceLauncher,都只有一个,换句话说,在numFreeSlots这个变量上面监听和睡眠的线程也只有一个,那为啥还要用notifyAll()函数呢? 真是让人费解啊!
吐槽下: csdn的博客编写界面真难用啊...
相关文章推荐
- HBase版本匹配问题:HBase-0.90.4与Hadoop-0.20.203.0
- Hadoop与之对应的Hbase版本问题 .
- Java的多线程NotiFyAll()唤醒线程时的顺序问题
- Hadoop运行jar包jdk版本冲突问题解决
- 关于hadoop2.4.2版本学习时遇到的问题
- kettle版本对应hadoop平台等问题
- 关于hive中jline版本与hadoop不匹配的问题
- 线程问题 之 登陆问题(91 Android版本SDK接入闪屏问题解决)
- 由一个HADOOP_HOME and hadoop.home.dir are unset报错引起的window环境连接调试远程hadoop的一系列问题,hadoop版本 2.8
- 黑马程序员——线程中等待唤醒机制及jdk5版本lock的深入思考
- Hadoop与之对应的Hbase版本问题
- hadoop的版本问题
- Linux 2. 关于pthread_cond_signal 唤醒两个线程的问题
- hadoop pig 版本不兼容问题
- day12线程间的通信,等待唤醒机制,生产者消费者问题。新锁lock(): 守护线程,interrupt()停止线程用,join()yield()
- 解决hbase和hdfs的hadoop版本不兼容问题
- hadoop、hbase、hive版本匹配问题
- hadoop 的版本问题
- hadoop,hbase,hive,zookeeper版本整合兼容性问题总结
- Hadoop 从零开始学习系列-旧版本hadoop工程升级新版本问题及解决