Multithreading allows two parts of the same program to run concurrently. This article discusses how to pull off this performance-improving feat in Java. It is excerpted from chapter 10 of the book Java Demystified, written by Jim Keogh (McGraw-Hill/Osborne, 2004; ISBN: 0072254548).
Marathon runners sometimes are faced with a dilemma when two major races fall during the same week because they have to choose which race to run in. They probably wish there was a way a part of them could go to one race and another part to the other race. That can’t happen—that is, unless the runner is a Java program, because two parts of the same Java program can run concurrently by using multithreading. You’ll learn about multithreading and how to run parts of your program concurrently in this chapter.
Multitasking
Multitasking is performing two or more tasks at the same time. Nearly all operating systems are capable of multitasking by using one of two multitasking techniques: process-based multitasking and thread-based multitasking.
Process-based multitasking is running two programs concurrently. Programmers refer to a program as a process. Therefore, you could say that process-based multitasking is program-based multitasking.
Thread-based multitasking is having a program perform two tasks at the same time. For example, a word processing program can check the spelling of words in a document while you write the document. This is thread-based multitasking.
A good way to remember the difference between process-based multitasking and thread-based multitasking is to think of process-based as working with multiple programs and thread-based as working with parts of one program.
The objective of multitasking is to utilize the idle time of the CPU. Think of the CPU as the engine of your car. Your engine keeps running regardless of whether the car is moving. Your objective is to keep your car moving as much as possible so you can get the most miles from a gallon of gas. An idling engine wastes gas.
The same concept applies to the CPU in your computer. You want your CPU cycles to be processing instructions and data rather than waiting for something to process. A CPU cycle is somewhat similar to your engine running.
It may be hard to believe, but the CPU idles more than it processes in many desktop computers. Let’s say that you are using a word processor to write a document. For the most part, the CPU is idle until you enter a character from the keyboard or move the mouse. Multitasking is designed to use the fraction of a second between strokes to process instructions from either another program or from a different part of the same program.
Making efficient use of the CPU may not be too critical for applications running on a desktop computer because most of us rarely need to run concurrent programs or run parts of the same program at the same time. However, programs that run in a networked environment, such as those that process transactions from many computers, need to make a CPU’s idle time productive.
分享到:
相关推荐
Multithreading in Java Chapter 9. Input/Output, Serialization and Cloning Chapter 10. Generics, java.util and other API Chapter 11. Network Programming Chapter 12. Applets Chapter 13. Event Handling ...
- **Concurrency:** Explore concurrency concepts and multithreading in Java. #### Conclusion The 1Z0-811 exam is a significant step towards achieving Oracle Java SE certification. By following a ...
其中,"multithreading applications in Win32.pdf"可能是一个关于Windows平台下多线程应用的补充材料,可能出自另一本书或文章,但与《Thinking in Java》的主题相关。多线程是Java编程中的一个重要部分,它允许...
Java多线程 多线程是Java的一项功能,它允许并发执行程序的两个或更多部分,以最大程度地利用CPU。... 睡眠时间将受某些特定于系统的粒度的影响,通常为1ms; 在Hibernate状态下,线程仍拥有已获取的同步锁; 睡眠
1: LEARNING JAVA 9 UNDERLYING PERFORMANCE IMPROVEMENTS 2: TOOLS FOR HIGHER PRODUCTIVITY AND FASTER APPLICATION 3: MULTITHREADING AND REACTIVE PROGRAMMING 4: MICROSERVICES 5: MAKING USE OF NEW APIS TO ...
实践中的Java并发和多线程 这是发行的的代码存储库。 它包含从头到尾完成视频课程所需的所有支持项目文件。 关于视频课程 多核处理器无处不在-从超级计算机到随身携带的移动设备。 这就是为什么现代开发人员必须知道...
It also discusses about exception handling, multithreading and java libraries. Further, it explains how to interact with client side applications like applets and handling events. The last section ...
Chapter 3 Fundamental Programming Structures in Java(新增批注共44条) Chapter 4 Objects and Classes(新增批注共55条) Chapter 5 Inheritance(新增批注共42条) Chapter 6 Interfaces and Inner Classes...
Java(TM) Performance and Scalability, Volume 1, provides invaluable advice that you will, no doubt, find useful in your coding. Presented in 48 concise lessons that target the most common and ...
s built-in support for multithreading through the Thread class and the Runnable interface. By creating and starting multiple threads, a Java application can perform several operations simultaneously, ...
3 FUNDAMENTAL PROGRAMMING STRUCTURES IN JAVA 4 OBJECTS AND CLASSES 5 INHERITANCE 6 INTERFACES AND INNER CLASSES 7 GRAPHICS PROGRAMMING 8 EVENT HANDLING 9 USER INTERFACE COMPONENTS WITH SWING 10 ...
If Java is the heavy metal of computer programming, then Python is the jazz that opens doors of freedom in software development. Both Java and Python are object-oriented programming languages. Both ...
Carver和Kuo-Chung Tai撰写,由John Wiley & Sons, Inc.出版,是一本深入探讨多线程编程的专著,尤其聚焦于Java和C++(包括Pthreads和Win32)语言环境下的多线程程序设计、测试与调试。本书不仅适合初学者作为入门...
This chapter introduces multithreading concepts and techniques in Java, including thread creation, synchronization, and inter-thread communication. Multithreading is particularly useful for improving...
1. **面向对象编程(Object-Oriented Programming, OOP)**:Java的核心是面向对象,书中深入解释了类、对象、继承、封装和多态等概念,让读者理解如何设计和实现复杂的软件系统。 2. **基础语法**:包括变量、数据...
Core Java offers a total immersion approach for CC++ programmer wanting to learn java. The course will use JDK(Java Development Kit) and covers all the ...Chapter 13 Managing Input/Output Files in Java
Chapter 3 Fundamental Programming Structures in Java(新增批注共44条) Chapter 4 Objects and Classes(新增批注共55条) Chapter 5 Inheritance(新增批注共42条) Chapter 6 Interfaces and Inner Classes...
This e-book provides an introduction to programming in Java. It covers the fundamental building blocks of the language and is designed for readers who have little or no programming background. A ...