<Introduction to Java Programming> Note 02
2016-06-15 18:15
513 查看
Chapter 30 Multithreading and Parallel Programming
You can create additional threads to run concurrent tasks in the program.
In Java, each task is an instance of the Runnable interface, also called a runnable object.
A thread is essentially an object that facilitates the execution of a task.
![](http://img.blog.csdn.net/20160615180107730?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20160615181502033?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
Since the Thread class implements Runnable, you could define a class that extends Thread and implements the run method, and then create an object from the class and invoke its start method in a client program to start the thread:
![](http://img.blog.csdn.net/20160615182213037?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
Instead of using the stop() method, you should assign null to a Thread variable to indicate that has stopped.
If a sleep method is invoked in a loop, you should wrap the loop in a try-catch block, as shown in (a) below. If the loop is outside the try-catch block, as shown in (b), the thread may continue to execute even though it is being
interrupted.
You can create additional threads to run concurrent tasks in the program.
In Java, each task is an instance of the Runnable interface, also called a runnable object.
A thread is essentially an object that facilitates the execution of a task.
Since the Thread class implements Runnable, you could define a class that extends Thread and implements the run method, and then create an object from the class and invoke its start method in a client program to start the thread:
Instead of using the stop() method, you should assign null to a Thread variable to indicate that has stopped.
If a sleep method is invoked in a loop, you should wrap the loop in a try-catch block, as shown in (a) below. If the loop is outside the try-catch block, as shown in (b), the thread may continue to execute even though it is being
interrupted.
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树