`
standalone
  • 浏览: 614314 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Multithreading in Java (2)

    博客分类:
  • java
阅读更多

 

Multithreading in Java - Overhead


(Page 2 of 10 )

The operating system must do extra work to manage multitasking. Programmers call this extra work overhead because resources inside your computer are used to manage the multitasking operation rather than being used by programs for processing instructions and data.

Process-based multitasking has a larger overhead than thread-based multitasking. In process-based multitasking, each process requires its own address space in memory. The operating system requires a significant amount of CPU time to switch from one process to another process. Programmers call this context switching, where each process (program) is a context. Additional resources are needed for each process to communicate with each other.

In comparison, the threads in thread-based multitasking share the same address space in memory because they share the same program. This also has an impact on context switching, because switching from one part of the program to another happens within the same address space in memory. Likewise, communication among parts of the program happens within the same memory location.

Threads

 

A thread is part of a program that is running. Thread-based multitasking has multiple threads running at the same time (that is, multiple parts of a program running concurrently). Each thread is a different path of execution.

Let’s return to the word processing program example to see how threads are used. Two parts of the word processor are of interest: The first is the part of the program that receives characters from the keyboard, saves them in memory, and displays them on the screen. The second part is the portion of the program that checks spelling. Each part is a thread that executes independently of each other, even though they are part of the same program. While one thread receives and processes characters entered into the keyboard, the other thread sleeps. That is, the other thread pauses until the CPU is idle. The CPU is normally idle between keystrokes. It is this time period when the spell checker thread awakens and continues to check the spelling of the document. The spell checker thread once again pauses when the next character is entered into the keyboard.

The Java run-time environment manages threads, unlike in process-based multitasking where the operating system manages switching between programs. Threads are processed asynchronously. This means that one thread can pause while other threads continue to process.

A thread can be in one of four states:

  • Running   A thread is being executed.

     

  • Suspended   Execution is paused and can be resumed where it left off.

     

  • Blocked   A resource cannot be accessed because it is being used by another thread.

     

  • Terminated   Execution is stopped and cannot be resumed.

     

All threads are not equal. Some threads are more important than other threads and are giving higher priority to resources such as the CPU. Each thread is assigned a thread priority that is used to determine when to switch from one executing thread to another. This is called context switching.

A thread’s priority is relative to the priority of other threads. That is, a thread’s priority is irrelevant if it is the only thread that is running. A lower-priority thread runs just as fast as a higher-priority thread if no other threads are executing concurrently.

Thread priorities are used when the rules of context switching are being applied. These rules are as follows:

  • A thread can voluntarily yield to another thread. In doing so, control is turned over to the highest-priority thread.

     

  • A higher-priority thread can preempt a lower-priority thread for use of the CPU. The lower-priority thread is paused regardless of what it’s doing to give way to the higher-priority thread. Programmers call this preemptive multitasking.

     

  • Threads of equal priority are processed based on the rules of the operating system that is being used to run the program. For example, Windows uses time slicing, which involves giving each high-priority thread a few milliseconds of CPU cycles, and keeps rotating among the high-priority threads. In Solaris, the first high-priority thread must voluntarily yield to another high-priority thread. If it doesn’t, the second high-priority thread must wait for the first thread to terminate.

     

Synchronization Multithreading occurs asynchronously, meaning one thread executes independently of the other threads. In this way, threads don’t depend on each other’s execution. In contrast, processes that run synchronously depend on each other. That is, one process waits until the other process terminates before it can execute.

 

Sometimes the execution of a thread is dependent on the execution of another thread. Let’s say you have two threads—one handles gathering login information, and the other validates a user’s ID and password. The login thread must wait for the validation thread to complete processing before it can tell the user whether or not the login is successful. Therefore, both threads must execute synchronously, not asynchronously.

Java enables you to synchronize threads by defining a synchronized method. A thread that is inside a synchronized method prevents any other thread from calling another synchronized method on the same object. You’ll learn how to do this later in the chapter.

分享到:
评论

相关推荐

    《Programming in Java Second Edition》高清完整英文PDF版

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

    thinking in Java

    其中,"multithreading applications in Win32.pdf"可能是一个关于Windows平台下多线程应用的补充材料,可能出自另一本书或文章,但与《Thinking in Java》的主题相关。多线程是Java编程中的一个重要部分,它允许...

    1Z0-811 Exam Guide to Have a Cakewalk in Oracle Java SE Certific

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

    JAVA核心技术卷一卷二(中文)之part2分卷

    Chapter 3 Fundamental Programming Structures in Java(新增批注共44条) Chapter 4 Objects and Classes(新增批注共55条) Chapter 5 Inheritance(新增批注共42条) Chapter 6 Interfaces and Inner Classes...

    Multithreading-in-java

    Java多线程 多线程是Java的一项功能,它允许并发执行程序的两个或更多部分,以最大程度地利用CPU。 这种程序的每个部分都称为线程。 因此,线程是进程中的轻量级进程。 可以使用两种机制来创建线程: 扩展Thread类...

    Java: High-Performance Apps with Java 9

    In 1999, with his wife Luda and two daughters, he emigrated to the USA and has been living in Colorado ever since, working as a Java programmer. In his free time, Nick likes to write and hike in the ...

    Java-Concurrency-Multithreading-in-Practice:Packt出版的《 Java Concurrency&Multithreading in Practice》

    实践中的Java并发和多线程 这是发行的的代码存储库。 它包含从头到尾完成视频课程所需的所有支持项目文件。 关于视频课程 多核处理器无处不在-从超级计算机到随身携带的移动设备。 这就是为什么现代开发人员必须知道...

    Java2核心技术卷I+卷2:基础知识(第8版) 代码

    Java As a Programming Platform 2 The Java “White Paper” Buzzwords 2 Java Applets and the Internet 7 A Short History of Java 9 Common Misconceptions about Java 11 Chapter 2: The Java ...

    A Textbook of Java Programming

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

    java面试题英文版及其答案

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

    Java Performance and Scalability

    This book was written with one goal in mind: to provide Java programmers with the expertise needed to build efficient, scalable Java code. The author shares his experience in server-side performance ...

    Java.to.Python 高清完整epub版

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

    Modern Multithreading

    Carver和Kuo-Chung Tai撰写,由John Wiley & Sons, Inc.出版,是一本深入探讨多线程编程的专著,尤其聚焦于Java和C++(包括Pthreads和Win32)语言环境下的多线程程序设计、测试与调试。本书不仅适合初学者作为入门...

    Core Java 9th Edition(Vol1,Vol2)

    Fundamental Programming Structures in Java Chapter 4. Objects and Classes Chapter 5. Inheritance Chapter 6. Interfaces and Inner Classes Chapter 7. Graphics Programming Chapter 8. Event Handling ...

    Core Java. Volume I. Fundamentals, 8th Edition JAVA核心技术1基础知识

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

    Java - A Beginner’s Guide - Sixth Edition - Herbert Schildt

    This chapter introduces multithreading concepts and techniques in Java, including thread creation, synchronization, and inter-thread communication. Multithreading is particularly useful for improving...

    The Java. Native

    • integrating a Java application with legacy code written in languages such as C or C++ • incorporating a Java virtual machine implementation into an existing application written in languages such ...

    Thinking_in_Java_4th_Edition_CN

    9. **多线程(Multithreading)**:Java支持多线程编程,书中会讲解线程的创建、同步和通信,以及线程池的使用。 10. **输入/输出流(I/O Stream)**:介绍如何在Java中进行文件读写,网络通信以及其他类型的输入...

Global site tag (gtag.js) - Google Analytics