`
lobin
  • 浏览: 425279 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Java interrupting a thread that is not alive need not have any effect

 
阅读更多

 

 

@Test
public void interrupt4b() {
	Thread t = new Thread() {
		public void run() {
			for (int i = 0; i < 10; i++) {
				if (isInterrupted()) {
					System.out.println("interrupted");
				} else {
					System.out.println("not interrupted");
				}
			}
			
			interrupt();
			
			for (int i = 0; i < 10; i++) {
				if (isInterrupted()) {
					System.out.println("interrupted");
				} else {
					System.out.println("not interrupted");
				}
			}
		}
	};
	
	t.start();
	
	try {
		t.join();
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	
	// thread t's status is not alive when code executed here.
	System.out.println("the interrupted status while thread's status is not alive:");
	for (int i = 0; i < 10; i++) {
		if (t.isInterrupted()) {
			System.out.println("interrupted");
		} else {
			System.out.println("not interrupted");
		}
	}
	
	if (t.isAlive()) {
		System.out.println("is alive");
	} else {
		System.out.println("is not alive");
	}
	
	for (int i = 0; i < 10; i++) {
		if (t.isInterrupted()) {
			System.out.println("interrupted");
		} else {
			System.out.println("not interrupted");
		}
	}
	
	System.out.println("the interrupted status while thread's status is not alive and call interrupt() to interrupt thread:");
	t.interrupt();
	
	for (int i = 0; i < 10; i++) {
		if (t.isInterrupted()) {
			System.out.println("interrupted");
		} else {
			System.out.println("not interrupted");
		}
	}
}

 

分享到:
评论

相关推荐

    Live-debug-a-running-Process

    Live debugging of a running process refers to the process of debugging a program that is currently executing, without interrupting its normal operation. This technique is particularly useful when ...

    acpi控制笔记本风扇转速

    is a region field. A buffer object is also allowed as the source operand. BZ 480 Fixed a problem where the Load ASL operator allowed the source operand to be an operation region of any type. It is ...

    java.核心技术.第八版 卷1

    This revised edition of the classic Core Java™, Volume I–Fundamentals, is the definitive guide to Java for serious programmers who want to put Java to work on real projects. Fully updated for the ...

    Java2核心技术卷I+卷2:基础知识(第8版) 代码

    Java 核心技术 卷1 Index Chapter 1: An Introduction to Java 1 Java As a Programming Platform 2 The Java “White Paper” Buzzwords 2 Java Applets and the Internet 7 A Short History of Java 9 ...

    大学英语B统考试题库.doc

    —Thank you, I hope I am not interrupting you. —No, ________. - **选项:**A. thank you B. not at all C. thanks D. let it alone - **答案解析:**被问到是否被打扰时,如果事实上并未被打扰,应礼貌地...

    JAVA线程, 2Ed (Source).rar

    这些例子可能涵盖了线程池(ThreadPoolExecutor)、线程优先级、守护线程(daemon thread)、中断线程(interrupting threads)、线程本地变量(ThreadLocal)等主题。通过实际操作这些源码,你将能够更好地理解Java...

    酒店常用服务英语.pdf

    - You're welcome./Not at all./That's all right./Don't mention it./It's my pleasure.:对客人的感谢表示回应。 - Welcome to our hotel, sir (madam).:欢迎光临本酒店。 - We're glad to have you here.:很...

    商务会议常用英语对话.doc

    - "We offer you our best prices, at which we have done a lot of business with other customers."(我们向你们报最优惠价,按此价我们已与其他客户做了大批生意。) - "Will you please tell us the ...

    礼仪培训之礼貌的句子.doc

    It is not." 明确否定,但依然保持礼貌。 4. 短时延误(少于五分钟) 在等待期间,向客人解释情况: - "Just a moment / one moment, please, Sir/Madam." 请稍等片刻。 - "This may take a few minutes, Sir/...

    quartz-2.1.1 完整源码

    Quartz是一个开源的作业调度框架,它完全由Java写成,并设计用于J2SE和J2EE应用中。它提供了巨大的灵 活性而不牺牲简单性。你能够用它来为执行一个作业而创建简单的或复杂的调度 What's New In Quartz Scheduler ...

    商务英语口语8000句.doc

    - "We offer you our best prices, at which we have done a lot business with other customers." 这是提供最优惠报价的表述,表明这是基于与其他客户交易的成功经验。 - "Will you please tell us the ...

    展览会所需的英语话术.pdf

    - "The quality of ours is as good as that of many other suppliers, while our prices are not high as theirs." 强调产品质量和价格优势。 - "You can rest assured." 让客户放心,保证产品或服务的可靠性。 ...

    可视化编程

    the package is designed to have following features: A graphic editor to visually edit the power system diagram, windows-based power system database to interactively manage the data, graphic ...

    iMON & iMEDIAN HD User's Guide(ENG)

    iMEDIAN HD is a straightforward yet powerful media player that can be controlled using only four directional buttons. It supports a wide range of media formats and devices, making it highly versatile ...

    Addison.Wesley.C++.by.Dissection.2002.pdf

    - **Interrupting a Program:** Explains how to interrupt a running program. - **Typing an End-of-File Signal:** Describes the use of EOF signals. - **Dr. P’s Prescriptions:** Offers practical advice ...

    Glossary of Circuit Breaker Terms

    #### AIC (Amperes Interrupting Capacity)(分断容量) 分断容量是指断路器在特定电压下能够安全切断的最大故障电流。它是衡量断路器性能的重要指标之一。 #### AIR (Amperes Interrupting Rating)(分断额定值) ...

    酒店英语常用语(培训)修改版.doc

    "和"Have a good day."都是增进情感交流的友好表达。 在酒店工作中,恰当使用这些英语表达能够增强与国际客户沟通的流畅性,提升服务质量,确保宾客得到满意体验。同时,要注意根据不同情境和客户的个人喜好,灵活...

    山东省华侨中学2016届高三英语10月月考试题

    7. "Florence in Italy, ________ beauty is beyond description, is the city that I have been dreaming of visiting."非限制性定语从句中,"beauty"是先行词,关系代词"whose"引导从句,表示“佛罗伦萨的美”。...

    FutureTask底层实现分析,有了FutureTask主线程要想获得工作线程(异步计算线程)的结果变得比较简单

    state 的值可以是 NEW、COMPLETING、NORMAL、EXCEPTIONAL、INTERRUPTING、CANCELLED 等几种状态。不同的状态对应不同的任务状态,例如 NEW 状态表示任务还没有执行,COMPLETING 状态表示任务已经完成等。 在 ...

Global site tag (gtag.js) - Google Analytics