Lecter Java: Program design - Chapter 12: Threads

In this chapter, we examine several Java mechanisms for creating, scheduling, managing, and running threads. After completing this unit, you should be able to: Develop programs with multiple, concurrent flows of control using threads; create threads that run at a time relative to the current time; create threads that run repeatedly;. | Threads Story so far Our programs have consisted of single flows of control Flow of control started in the first statement of method main() and worked its way statement by statement to the last statement of method main() Flow of control could be passed temporarily to other methods through invocations, but the control returned to main() after their completion Programs with single flows of control are known as sequential processes Processes The ability to run more than one process at the same time is an important characteristic of modern operating systems A user desktop may be running a browser, programming IDE, music player, and document preparation system Java supports the creation of programs with concurrent flows of control – threads Threads run within a program and make use of its resources in their execution Lightweight processes Processes Multithread processing Timer and TimerTask Among others, Java classes and support the creation and . | Threads Story so far Our programs have consisted of single flows of control Flow of control started in the first statement of method main() and worked its way statement by statement to the last statement of method main() Flow of control could be passed temporarily to other methods through invocations, but the control returned to main() after their completion Programs with single flows of control are known as sequential processes Processes The ability to run more than one process at the same time is an important characteristic of modern operating systems A user desktop may be running a browser, programming IDE, music player, and document preparation system Java supports the creation of programs with concurrent flows of control – threads Threads run within a program and make use of its resources in their execution Lightweight processes Processes Multithread processing Timer and TimerTask Among others, Java classes and support the creation and scheduling of threads Abstract class Timer has methods for creating threads after either some specified delay or at some specific time public void schedule(TimerTask task, long m) Runs () after waiting m milliseconds. public void schedule(TimerTask task, long m, long n) Runs () after waiting m milliseconds. It then repeatedly reruns () every n milliseconds. public void schedule(TimerTask task, Date y) Runs () at time t. A thread can be created By extending TimerTask and specifying a definition for abstract method run() Running after a delay Class DisplayCharSequence extends TimerTask to support the creation of a thread that displays 20 copies of some desired character (., “H”, “A”, or “N”) Using DisplayCharSequence public static void main(String[] args) { DisplayCharSequence s1 = new DisplayCharSequence('H'); DisplayCharSequence s2 = new DisplayCharSequence('A'); DisplayCharSequence s3 = new DisplayCharSequence('N'); } Defining DisplayCharSequence import .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
2    531    2    28-04-2024
Đã 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.