您的位置:首页 > 编程语言 > Java开发

<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.





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 线程