In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains how to understand the polling and interruption of Java asynchronous events. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to understand the polling and interruption of Java asynchronous events.
CPU spends almost all its time fetching instructions from memory and running them. However, CPU and main memory are just two of the many components in a computer hardware system. A complete system also includes other devices, such as:
Hard disk or solid state disk, used to store programs and data files. Note that only a small amount of information is stored in main memory, and the information can be saved only when the computer is powered on. Hard drives or solid state drives are used to store large amounts of information. But before the program can actually run, the program must load the data from the hard disk or solid state disk into the main memory. Hard drives store data in rotating disks (spinning magnetic disk), while solid-state drives store data in pure electronic devices, which do not require rotation or any mechanical motion.
Keyboard and mouse, input to the user.
Monitors and printers are used to display the output of the computer.
An audio output device that enables the calculation to play sound.
A network interface that enables computers to communicate with other networked computers, which are connected by wired or wireless means.
A scanner that converts pictures into binary code that can be stored and manipulated on a computer.
The devices listed above are all open ended, and computers are designed to easily extend computers by adding new devices. CPU must somehow communicate with these devices and control them, and it can only do this by running machine language instructions (that's all it can do). The implementation of this process is that each device in the system corresponds to a device driver, which is some application software and runs when CPU interacts with the device. Installing a new device in a system usually requires two steps: checking the physical device into the computer, and then installing the device driver software. Without device drivers, physical devices will become useless because CPU cannot communicate with physical devices.
A computer system consisting of many devices is usually organized by connecting these devices to one or more buses. A bus is a set of wires that carry information about the devices connected to these wires. The information carried by the wire includes data, address and control signal. Address information directs data to a specific device or to a specific register or to a specific location within a particular device. The control signal can be used by one device to notify another device that the data can be obtained on the data bus. A very simple computer system can be organized like this:
Today, devices such as keyboards, mice and network interfaces can generate input data and require CPU to process it. So how does CPU know that the data has arrived? A simple and not ideal approach is to have CPU constantly check to see if the data has arrived and process it every time it is found. This method is called polling because CPU needs to continuously poll the input device to detect whether there is any input data to be processed. Unfortunately, although polling is simple, it is also inefficient. Because CPU spends a lot of time waiting for input.
In order to improve efficiency, interrupts are usually used instead of polling. Interrupts are signals sent to CPU by other devices. In order to respond to the interrupt signal, CPU shelves the transaction it is processing to respond to the interrupt signal. Once CPU finishes handling interrupts, it returns and handles transactions that are shelved when interruptions occur. For example, when you press a key on the keyboard, the keyboard interrupt will be sent to CPU. CPU then responds to the interrupt signal by interrupting the transaction in progress and reads and processes the key information you press. * CPU will return to the task you were performing before you pressed the key.
In addition, you need to know that this interrupt mechanism is implemented entirely in hardware: the device simply represents the interrupt information by connecting the wire. CPU is designed so that when the wire is connected, it will save the transaction information that is being processed (save the on-site information) so that it can later return to the same state (restore the site). This information contains important internal registers such as program counters. CPU then jumps to some preset memory location and executes the instructions stored there. These instructions constitute an interrupt processor that performs the necessary processing to respond to interrupts (this interrupt processor is part of the driver software of the device that sends the signal). An instruction of the interrupt processor uses the previously saved state information to instruct the CPU to jump back to the scene.
Interrupts enable CPU to handle asynchronous events. In a periodic read-execute cycle, events occur in a predetermined order; all events occur "synchronously" with other events. Interrupts make it possible for CPU to handle events efficiently using "async", when the timing of events is unpredictable.
As another example of using interrupts, think about what happens when CPU needs to access data stored on a hard disk. CPU can only access data in memory directly. So the data must be copied to memory before accessing the data on the hard disk. Unfortunately, the hard drive is slow compared to the speed of the CPU. When CPU needs data on the hard disk, it sends a signal to the hard drive to locate and prepare the data (this signal is sent asynchronously under a regular program). CPU then goes on to do some other tasks instead of an unpredictable long wait, which will be completed by the hard drive. When the hard drive prepares the data, it sends an interrupt signal to the CPU. The interrupt processor then reads the request data.
By now you may have noticed that interrupts can only be seen when the CPU has multiple tasks to perform. If the CPU has only one task to perform, the interrupt will also spend time polling input or waiting for the hard drive operation to complete. All modern computers use multitasking (multitasking) to perform multiple tasks at a time. Some computers can be used by multiple users at the same time. Because CPU is fast, it can quickly switch from one user to another and work for each user for a short period of time. This use of multitasking is called time-sharing (timesharing). Nevertheless, only one user's modern personal computer also uses multitasking. For example, while the clock program continuously displays the time and downloads files online, users may also be using computers to write papers.
Each single task that CPU performs is called a thread (or a process; there is a technical difference between threads and processes, but this difference is not important here, because the thread we are talking about is a thread in Java). Many CPU can run multiple threads at the same time-- these CPU contain multiple cores and each core can run one thread, but the number of threads running at the same time is limited. Often because there are too many threads to run all threads at the same time, the computer must be able to switch from one thread to another, just as a time-sharing computer switches from one user to two users. Typically, a running thread runs all the time unless one of these situations occurs:
Threads voluntarily yield control, giving other threads a chance to run.
Threads may have to wait for some asynchronous events to occur. For example, a thread may need some data on the hard disk, or it may be waiting for the user to press the key bit. When a thread is waiting, we say it is blocked. At this point, if there are other threads, they have the opportunity to run. When a waiting event occurs, the interrupt will "wake up" the blocking thread to continue running.
A thread may be hung up because it runs out of time slices allocated to it so that other threads can run. Not all computers can "force" threads to be suspended in this way. Those that can be forced to hang are called preemptive multitasking systems (preemptive multitasking). To use preemptive multitasking, a computer needs a special timing device that can generate interrupts periodically, such as 100 times per second. When a timed interrupt occurs, the CPU can switch from one thread to another, regardless of whether the thread is executing or not. All modern desktops and laptops, even ordinary smartphones and tablets, are using preemptive multitasking.
Ordinary users, and even ordinary programmers, do not need to deal with interrupts and interrupt processors. They can focus on different tasks or on the threads they want the computer to execute. The details of how computers accomplish these tasks are not important to them. In fact, most users and many programmers can ignore threads and multitasking. However, as computers become more powerful and multitasking and multiprocess are used more and more, threads have become more and more important. In fact, the ability to use threads will soon become a basic ability for programmers. Fortunately, Java provides good support for threads, which are built into the Java programming language as a basic program concept. Programming with threads will be covered in Chapter 12.
In general, the basic concept of asynchronous events is just as important in Java as in modern programming. Even if programmers don't actually deal directly with interrupts, they often find themselves writing event handlers. An event handler is similar to an interrupt handler in that it is called when a particular event occurs. Compared with many traditional, straight-through, synchronous programming, "event-driven programming" has a different experience. We will start with traditional programming, which is still used to write programs with a single task. However, we will return to threads and events in Chapter 6.
By the way, software that performs all the interrupt handling, manipulates the interaction between the user and the hardware device, and controls which thread can run is called the operating system. The operating system is the most basic and important software, without which the computer cannot operate normally. Other programs such as word processors and Web browsers depend on the operating system. Common operating systems include Linux, various versions of Windows, and Mac OS.
Thank you for your reading, the above is the content of "how to understand the polling and interruption of Java asynchronous events". After the study of this article, I believe you have a deeper understanding of how to understand the polling and interruption of Java asynchronous events, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 300
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.