`

about Thread.join method

阅读更多
import java.util.Timer;
import java.util.TimerTask;

public class Worker extends Thread {

	private volatile boolean quittingTime = false;

	public void run() {

		while (!quittingTime)

			pretendToWork();

		System.out.println("Beer is good");

	}

	private void pretendToWork() {

		try {

			Thread.sleep(300); // Sleeping on the job?

		} catch (InterruptedException ex) {
		}

	}

	// It's quitting time, wait for worker - Called by good boss

	synchronized void quit() throws InterruptedException {

		quittingTime = true;

		join();

	}

	// Rescind quitting time - Called by evil boss

	synchronized void keepWorking() {

		quittingTime = false;

	}

	public static void main(String[] args) throws InterruptedException {

		final Worker worker = new Worker();

		worker.start();

		Timer t = new Timer(true); // Daemon thread

		t.schedule(new TimerTask() {

			public void run() {
				worker.keepWorking();
			}

		}, 500);

		Thread.sleep(400);

		worker.quit();

	}

}

 

If you run the above code, the program just hangs repeatably. why? anwser is located in Java Puzzlers puzzle 77 The Lock Mess Monster. as join method executed in the main thread just waits and releases the lock, the daemon thread accquires the lock and set it to run forever.

 

The answer concerns the implementation of Thread.join . It can't be found in the documentation for this method, at least in releases up to and including release 5.0. Internally, Thread.join calls Object.wait on the Thread instance representing the thread being joined. This releases the lock for the duration of the wait. In the case of our program, this allows the timer thread, representing the evil boss, to waltz in and set quittingTime back to false , even though the main thread is currently executing the synchronized quit method. As a consequence, the worker thread never sees that it's quitting time and keeps running forever. The main thread, representing the good boss, never returns from the join method.

 

here, i think the join method though executed by the main thread, and it waits for the main thread to die, but main thread wont go to death until the worker thread finishes its life, is it?

分享到:
评论

相关推荐

    AboutView.vue

    AboutView.vue

    All about AutoFilter.xlt

    All about AutoFilter.xlt

    about Eason.html.mno

    about Eason.html.mno

    PSO2.rar_About Method_s method_swarm

    标题中的"PSO2.rar_About Method_s method_swarm"提到了“粒子群优化法”(Particle Swarm Optimization, PSO),这是一种基于群体智能的优化算法,由James Kennedy和Russell E. Eberhart在1995年提出。该算法模拟了...

    convolutional-encoder-and-decoder.rar_About Method

    this code is about the error control coding using matlabcode.the method that i used is convolution coding .

    The basics about SLAM.pptx

    The basics about SLAM.pptx,经典资源

    about.html

    droid at school, useful to capture android screen shot

    AboutTime 时间校对工具

    关于AboutTime时间校对工具,这是一款专用于网络时间同步的软件,它的主要功能是帮助用户确保计算机系统的时间准确无误。在计算机领域,时间的准确性对于许多应用和服务至关重要,例如金融交易、日志记录、分布式...

    PowerShellPack

    About_method.help.txt About_pssnapins.help.txt About_namespace.help.txt About_object.help.txt About_operator.help.txt About_parameter.help.txt About_parsing.help.txt About_path_syntax.help.txt About_...

    精彩编程与编程技巧-显示Windows系统的标准ABOUT窗口...

    本篇文章将深入解析如何在Windows系统中调用标准的ABOUT对话框,这一技术对于提升用户界面的友好性和软件的专业性具有显著作用。通过分析给定文件中的代码片段,我们将了解其工作原理,以及如何在自己的项目中应用这...

    100 Things You Should Know About Sales....zip

    sap press doc 解压密码:abap_developer

    All.About.PDF.3.2000.rar

    "All About PDF 3.2000" 是一个针对PDF文件处理的软件工具,提供了全面的PDF管理功能,如PDF合并、拆分、转换以及加密等。 PDF合并功能允许用户将多个PDF文件整合到一个单一的文档中,这对于整理报告、论文或者手册...

    Android代码-一个简单的可定制的About页面.

    Attribouter is a lightweight "about screen" for Android apps, built to allow developers to easily give credit to a project's contributors and open source libraries, while matching the style of their ...

    About Typora.md

    现在很多人都用word写文档,调格式很麻烦,用Typora,不会用,我就用Typora整理了Markdown的语法供大家参考

    about:Sourcegraph博客,功能公告和网站(about.sourcegraph.com)

    Sourcegraph about.sourcegraph.com网站 使用Gatsby渲染由Netlify构建和部署的静态站点。 快速开始 什么 命令 自动预览about.sourcegraph.com cd website && yarn install && yarn start 最终评论about....

    Chapter_5.rar_About Method

    1. **微分方程的数值解法**:数值方法处理微分方程时,常用的方法有欧拉法(Euler's method)、龙格-库塔法(Runge-Kutta methods)以及有限差分法。这些方法通过近似连续函数的导数来离散化微分方程,然后求解得到...

    about.me——在线名片网站简约设计中的生存之道.docx

    关于在线名片网站about.me,其简约设计是其在竞争激烈的市场中脱颖而出的关键。about.me作为一个在线展示个人信息的平台,允许用户自定义个性化页面,包括个人简介、联系方式、社交媒体链接等,通过调整个人照片和...

    All.About.CAPTCHAs.Decoding.CAPTCHAs.for.Fun

    All About CAPTCHA’s. This eBook will teach you how to identify weaknesses and exploit CAPTCHA’s from beginning to end. Welcome to the practical guide to decoding CAPTCHA's. Everything you need to ...

    ABOUT3.cpp

    VC++ Custom Controls控件添加后可以编译通过但是运行不显示,删除控件后OK的问题的解决办法

Global site tag (gtag.js) - Google Analytics