Java programming-asynchronous events: polling and interruption

Source: Internet
Author: User

Java programming-asynchronous events: polling and interruption

The CPU spends almost all its time obtaining commands from the memory and running them. However, the CPU and primary memory are only two of the many components in the computer hardware system. A complete system also contains other devices, such:

  • Hard disk or solid state disk, used to store programs and data files. Note that the primary storage only saves a small amount of information, and the information can be saved only when the computer is powered on. Hard disks or solid state disks are used to permanently store a large amount of information. However, before the program runs, the program must load data from the hard disk or solid state disk to the primary storage. The hard disk stores data in a rotating disk, while the solid state disk stores the data in pure electronic devices, which does not require rotation or any mechanical movement.

  • Keyboard and mouse.

  • Displays and printers are used to display computer output.

  • Audio output device that enables computing to play sound.

  • Network Interfaces enable computers to communicate with other networked computers, which are connected through wired or wireless networks.

  • A scanner converts an image into binary code that can be stored and operated on a computer.

The devices listed above are all open ended), and the computer is designed to easily expand the computer by adding new devices. The CPU must communicate with and control these devices in some way, and it can only achieve this by running machine language commands. This is what it can do ). The implementation of this process is that each device in the system corresponds to a device driver, which is used by some application software and runs when the CPU interacts with the device. Installing a new device in the system usually requires two steps: Check the physical device in the computer, and then install the device driver software. If there is no device driver, the physical device will become useless because the CPU cannot communicate with the physical device.

Computer systems composed of multiple devices usually connect these devices to one or more bus for organization. A bus is a set of wires that carry various information about devices connected to these wires. Information Carried by wires includes data, addresses, and control signals. Address information directs data to a specific device, register, or location in a specific device. A control signal can be used to notify another device to obtain the data on the Data Bus. A very simple computer system can be organized as follows:

Today, keyboard, mouse, network interface, and other devices can generate input data and need the CPU to process the data. So how does the CPU know that the data has arrived? A simple and not ideal method is to keep the CPU checking whether the data has arrived and process the data each time it discovers the data. Because the CPU needs to continuously poll the input device to check whether input data needs to be processed, this method is called polling. Unfortunately, although polling is simple, it is equally inefficient. Because the CPU spends a lot of time waiting for input.

In order to improve efficiency, we usually use interruptions instead of polling. Interruption is the signal sent from other devices to the CPU. In response to the interrupt signal, the CPU suspends the transaction being processed to respond to the interrupt signal. Once the CPU is interrupted, it returns and processes the transactions that are shelved when the interruption occurs. For example, when you press a key on the keyboard, the broken Keyboard will be sent to the CPU. Then, the CPU responds to the interrupt signal by interrupting the transaction being processed, and reads and processes the key bit information you press. Finally, the CPU returns the task that is being executed before you press the key.

In addition, you need to know that the interrupt mechanism is fully implemented by hardware: The device simply uses the wire to indicate the interrupt information. The CPU is designed to save the transaction information being processed and save the on-site information when the wire is connected ). This information includes program counters and other important internal registers. Then, the CPU jumps to Some preset memory locations and executes the commands stored there. These commands constitute an interrupt processor used to perform some necessary processing to respond to interruptions. This interrupt processor is part of the driver software of the device that sends signals ). The last instruction of the interrupt processor uses the previously saved status information to indicate that the CPU is switched back to the scene.

Interrupt enables the CPU to process asynchronous events. In a regular read-Execute loop, events occur in a predetermined order, and all events are "synchronized" with other events. Interruption makes it possible for the CPU to use "Asynchronous" to efficiently process events. The event occurrence time is unpredictable.

As another example of interruption, consider what happens when the CPU needs to access the data stored on the hard disk? The CPU can only directly access data in the memory. Therefore, data must be copied to the memory before accessing the data on the hard disk. Unfortunately, the hard disk speed is slower than the CPU speed. When the CPU needs data on the hard disk, it sends a signal to the hard drive asking it to locate and prepare the data. This signal is sent asynchronously in a conventional program ). Then, the CPU continues to do some other tasks instead of making unpredictable long waits, which will be completed by the hard drive. When the hard drive has data ready, it sends an interrupt signal to the CPU. Then the interrupt processor will read the request data.

Now you may have noticed that the interruption can only be realized when the CPU has multiple tasks to be executed. If the CPU has only one task to execute, interruption also takes time on polling input or waiting for the completion of hard drive operations. All modern computers use multi-task processing (multitasking) to execute multiple tasks at a time. Some computers can be used by multiple users at the same time. Because the CPU speed is very fast, it can quickly switch from one user to another, and work for each user for a short time in turn. This type of multitasking is called the time-sharing system timesharing ). Nevertheless, only one user's modern PC also uses multitasking. For example, when the clock program keeps displaying the time and downloading files online, users may also use computers to write papers.

Each single task executed by the CPU is called a thread or a process. There is a technical difference between the thread and the process, but this difference is not important, because the thread we want to discuss is a thread in Java ). Many CPUs can run multiple threads at the same time. These CPUs contain multiple "cores" and each core can run one thread. However, the number of threads running at the same time is limited. Because there are too many threads to run all the 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. Normally, a running thread will run continuously unless one of the following situations occurs:

  • The thread voluntarily gives control over yield and gives other threads the chance to run.

  • The thread may have to wait for some asynchronous events. For example, a thread may need some data on the hard disk, or it may be waiting for the user to press the key. When the thread is waiting, we call it blocked ). At this time, if there are other threads, they will have the opportunity to run. When a waiting event occurs, the interruption will "wake up" the blocked thread to continue running.

  • The thread may be hung up because it is exhausted and the time slices allocated to it so that other threads can run. Not all computers can force threads to be suspended in this way. Preemptive multitasking, a preemptive multitasking system that can be "forced" to be suspended ). To use preemptible multitasking, a computer needs a special scheduled device that regularly interrupts, such as generating 100 times a second. When a scheduled interrupt occurs, the CPU can switch from one thread to another, regardless of whether the thread is being executed. All modern desktops and laptops, even ordinary smartphones and tablets, are using preemptible multitasking.

Common users and even programmers do not need to deal with interrupt and interrupt processors. They can concentrate on processing different tasks or the threads they want the computer to execute. How Computers complete these tasks is not important to them. In fact, most users and many programmers can ignore threads and tasks. However, as computers become more and more powerful, multi-task and multi-process applications become more and more important, threads become more and more important. In fact, the ability to use threads will soon become a basic capability of programmers. Fortunately, Java provides good support for threads. It regards threads as a basic program concept and is built into the Java programming language. In chapter 2, we will discuss how to use thread programming.

Generally, what is equally important in Java and modern programming is the basic concept of asynchronous events. Even if programmers do not actually deal with interruptions directly, they often find that they are writing event processors. The event processor is similar to the interrupt processor. It is called when a specific event occurs. Compared with many traditional, straight-through, and Synchronous Programming, "event-driven programming" has a different experience. We will start from the traditional programming, and they are still used to write a single task program. However, we will explain the threads and events in Chapter 6th.

By the way, the software that executes all interrupt processing, controls the interaction between users and hardware devices, and controls which thread can run is called the operating system. The operating system is the most basic and important software and cannot operate normally without it. Other programs such as the text processor and Web browser depend on the operating system. Common operating systems include Linux and various versions of Windows and Mac OS.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.