下面的例子SimpleThreads中用到了2-5节中的几个方法和概念。
SimpleThreads中有两个线程,一个是所有Java程序都有的主线程main,另一个就是在主线程中创建的runnble对象——循环输出消息的线程t,t在主线程中调用了join方法,因此主线程需等待线程t的完成。但如果t线程运行时间过长(本例中用patience加以控制),主线程就会中断。
在本例中有许多输出语句, 用以反映线程运行状态及线程对interrupt异常的反应。
public class SimpleThreads { // Display a message, preceded by // the name of the current thread static void threadMessage(String message) { String threadName = Thread.currentThread().getName(); System.out.format("%s: %s%n", threadName, message); } private static class MessageLoop implements Runnable { public void run() { String importantInfo[] = { "Mares eat oats", "Does eat oats", "Little lambs eat ivy", "A kid will eat ivy too" }; try { for (int i = 0; i < importantInfo.length; i++) { // Pause for 4 seconds Thread.sleep(4000); // Print a message threadMessage(importantInfo[i]); } } catch (InterruptedException e) { threadMessage("I wasn't done!"); } } } public static void main(String args[]) throws InterruptedException { // Delay, in milliseconds before // we interrupt MessageLoop // thread (default one hour). long patience = 1000 * 60 * 60; // If command line argument // present, gives patience // in seconds. if (args.length > 0) { try { patience = Long.parseLong(args[0]) * 1000; } catch (NumberFormatException e) { System.err.println("Argument must be an integer."); System.exit(1); } } threadMessage("Starting MessageLoop thread"); long startTime = System.currentTimeMillis(); Thread t = new Thread(new MessageLoop()); t.start(); threadMessage("Waiting for MessageLoop thread to finish"); // loop until MessageLoop // thread exits while (t.isAlive()) { threadMessage("Still waiting..."); // Wait maximum of 1 second // for MessageLoop thread // to finish. t.join(1000); if (((System.currentTimeMillis() - startTime) > patience) && t.isAlive()) { threadMessage("Tired of waiting!"); t.interrupt(); // Shouldn't be long now // -- wait indefinitely t.join(); } } threadMessage("Finally!"); } }运行结果:
main: Starting MessageLoop thread main: Waiting for MessageLoop thread to finish main: Still waiting... main: Still waiting... main: Still waiting... main: Still waiting... main: Still waiting... Thread-0: Mares eat oats main: Still waiting... main: Still waiting... main: Still waiting... main: Still waiting... Thread-0: Does eat oats main: Still waiting... main: Still waiting... main: Still waiting... main: Still waiting... Thread-0: Little lambs eat ivy main: Still waiting... main: Still waiting... main: Still waiting... main: Still waiting... Thread-0: A kid will eat ivy too main: Finally!
偷偷把patience改掉之后呢:
long patience = 1000 * 60 * 60;改为 long patience = 1000;
运行效果是这样的:
main: Starting MessageLoop thread main: Waiting for MessageLoop thread to finish main: Still waiting... main: Tired of waiting! Thread-0: I wasn't done! main: Finally!
相关推荐
2.5线程安全的Singleton 实现.. . . . . . . . . . . . . . . . . . . . . . . . . 48 2.6sleep(3) 不是同步原语. . . .. . . . . . . . . . . . . . . . . . . . . . . . 50 2.7归纳与总结. . . . . . . . . . . . ...
- 分析了在Java程序中使用线程的原因,包括提高程序性能、实现并发操作等。 - **1.4 总结** - 概括了本章的主要内容,并为读者理解后续章节打下基础。 #### 第二部分:Java线程API - **2.1 使用Thread类创建线程*...
Java非对称加密源程序代码实例,本例中使用RSA加密技术,定义加密算法可用 DES,DESede,Blowfish等。 设定字符串为“张三,你好,我是李四” 产生张三的密钥对(keyPairZhang) 张三生成公钥(publicKeyZhang...
- 参考官方文档和示例代码熟悉API的基本使用方法。 - 利用Javadocs等工具加深理解。 - 尝试简单的概率网络构建和推理实验。 - **进阶学习:** - 学习更高级的功能,如网络结构学习、参数优化等。 - 探索...
Java是一种广泛使用的面向对象的编程语言,以其跨平台、高性能和强大的库支持而闻名。"Java经典10个例子"通常会涵盖Java语言的核心概念和常用功能,旨在帮助初学者理解并掌握Java编程的关键点。以下是对这十个经典...
2. **面向对象**:Java是一种纯面向对象的语言,几乎所有的东西都是对象,这种设计方式有助于提高代码的重用性和灵活性。 3. **分布性**:Java语言支持数据分布和操作分布,这使得开发者能够轻松创建分布式应用程序...
### 多线程并发代码的IntelliJ IDEA中调试方法 #### 一、代码解析 在探讨如何使用IntelliJ IDEA进行多线程代码调试之前,先了解代码本身的功能。 **1.1 代码功能** 代码的主要目的是计算两个非常大的数字的阶乘...
5. 线程同步与通信:虽然在这个项目中没有涉及,但理解`synchronized`关键字和`wait()`、`notify()`方法对处理更复杂的多线程问题至关重要。 通过这个项目,开发者不仅可以提高对多线程的理解,还能进一步掌握Java ...
8.1.2 在Eclipse中使用包 194 8.1.3 天上掉下个package 197 8.1.4 包带来了什么? 197 8.2 import语句:化繁为简 200 8.2.1 import语句 200 8.2.2 一网打尽包中所有类 201 8.2.3 import语句带来的小问题 202 ...
8.1.2 在Eclipse中使用包 194 8.1.3 天上掉下个package 197 8.1.4 包带来了什么? 197 8.2 import语句:化繁为简 200 8.2.1 import语句 200 8.2.2 一网打尽包中所有类 201 8.2.3 import语句带来的小问题 202 ...
3.9.6 例子:在视图中使用java Script 68 3.10 在表单中使用视图 70 3.10.1 在表单中嵌入视图 70 3.10.2 创建视图模板 70 3.10.3 例子:使用表单在视图中添加 功能 71 3.10.4 例子:使用视图在表单中添加 功能 73 ...
3.9.6 例子:在视图中使用java Script 68 3.10 在表单中使用视图 70 3.10.1 在表单中嵌入视图 70 3.10.2 创建视图模板 70 3.10.3 例子:使用表单在视图中添加 功能 71 3.10.4 例子:使用视图在表单中添加 功能 73 ...
这个例子展示了如何创建一个简单的Java类、定义主方法和输出文本。 ```java public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } ``` 这个简单的...
A.1.3 传递和使用Java对象 A.1.4 JNI和Java违例 A.1.5 JNI和线程处理 A.1.6 使用现成代码 A.2 微软的解决方案 A.3 J/Direct A.3.1 @dll.import引导命令 A.3.2 com.ms.win32包 A.3.3 汇集 A.3.4 编写回调函数 A.3.5 ...