Đang chuẩn bị liên kết để tải về tài liệu:
Java Programming for absolute beginner- P21

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

Java Programming for absolute beginner- P21:Hello and welcome to Java Programming for the Absolute Beginner. You probably already have a good understanding of how to use your computer. These days it’s hard to find someone who doesn’t, given the importance of computers in today’s world. Learning to control your computer intimately is what will separate you from the pack! By reading this book, you learn how to accomplish just that through the magic of programming. | JavaProgAbsBeg-10.qxd 2 25 03 8 56 AM Page 358 k 0 c c 0 2 V Implementing the Runnable Interface There is another way to create a thread. Instead of extending the Thread class you can implement the Runnable interface. The Runnable interface requires that you override one method. You guessed it the run method. To run a Runnable object in a thread you pass it to the Thread Runnable constructor. When you invoke start on the thread it results in a call to the Runnable s run method. The Thread class itself implements the Runnable interface. That s why when you extend Thread you implement the run method. 0 a c E E s k a o k a s s The RunnableTest application is an example of how to implement the Runnable interface to create a thread. Its run method is exactly the same as the ThreadTest class s run method. It just counts to 10. To get this thread started you need to create a new Thread object called t in the main method This Runnable object is passed into its constructor and then its start method is invoked. Here is a listing of the source code RunnableTest Demonstrates how to implement the Runnable interface public class RunnableTest implements Runnable must implement the run method public void run for int i 1 i 10 i System.out.println i I public static void main String args RunnableTest test new RunnableTest Construct a thread with this Runnable Thread t new Thread test start the thread t.start I I You can see the output of the RunnableTest application in Figure 10.3. bjcK What s the difference between extending Thread and implementing Runnable Why would you choose one over the other Generally it is better to implement - x . the Runnable interface. One good reason for this is that if you are defining a subclass of another class such as GUIFrame you can t also subclass the Thread class. Inheritance can come from only one direct superclass. To get around this problem you can extend GUIFrame and implement the Runnable interface too. team Line - live informative Non-cost .

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.