The implementation of the shared in Java

xiaoxiao2021-03-06  45

There is an implementation and application in Java in Java. The application author in Java is introduced by the author. The IPC basically includes shared memory, signal light operation, message queue, signal processing and other parts, is a very important tool for development applications. The key to sharing the memory IPC mechanism, which has unique advantages for data sharing, system quick query, dynamic configuration, reducing resource cost, etc. For the UNIX system, the sharing is divided into two types of general shared memory and image file sharing memory, and corresponding Windows, actually only one image file sharing. So Java applications are also only created by image file sharing memory. In the Java language, there is basically not mentioned to share memory concepts, but in some applications, shared memory is really useful, such as in a distributed application in Java language, there is a large number of distributed shared objects, a lot You need to query the status of these objects to see if the system runs properly or understands the current statistics and status of these objects. If you use network communication, it is clear that the additional burden of the application will also increase some unnecessary application programming. If you use a shared memory, you can directly view the status data and statistics of the object, thereby reducing some unnecessary trouble. The use of shared memory has the following features: You can be accessed by multiple processes; the process of reading and writing cannot be written during execution of read and write operations; multiple processes can alternately perform write operations for a shared memory; After a process executes the write operation of the memory, it does not affect the access to the memory. At the same time, other processes are visible to the updated memory. If the abnormality exits when the process is written, the write operation is prohibited automatically. Relatively sharing files, the convenience and efficiency of data access has additionally, the use of shared memory has the following case: exclusive write operation, the corresponding written operation waiting queue. The exclusive write operation itself does not have data consistency problems. Shared write operation, corresponding to the shared write operation waiting queue. The shared write operation should pay attention to preventing the consistency problem of data. Exclusive read operation, correspondingly have shared read operation waiting queue; shared read operation, the corresponding shared read operation is waiting for the queue. In general, we are only concerned about the first two situations. 2 Shared Inside the implementation of the implementation of Java in JDK1.4, the class mappedByteBuffer provides better ways for us to share memory. The buffer is actually a memory image of a disk file. The changes will remain synchronous, ie, memory data changes, which are reflected in the disk file, which will effectively ensure the implementation of shared memory. Connect the shared memory and disk files is the file channel class: FileChannel. The addition of this class is the JDK's access method for unified external devices (files, network interfaces, etc.), and enhances the security of the multi-threaded to save the same file. For example, the read and write operations are uniform into READ and WRITE. Here is just to build shared memory, which establishes a channel between shared memory and disk files. Open a file to create a file channel to use the method in the RandomaccessFile class GetChannel. This method will return directly to a file channel.

Since the corresponding file is set to a random access file, one hand can read and write two operations, on the other hand, using it does not destroy the contents of the image file (if you open an image file directly with FileoutPutStream) The size is set to 0, and of course the data will be lost). Here, if FileOutputStream and FileInputStream are not ideal for implementation of shared memory, because these two classes simultaneously achieve free reading and writing operations. The following code implements the above function, its role similar to the MMAP function in the UNIX system. // Get a read-only random access file object randomaccessfile rafile = new randomaccessfile (filename, "r"); // Get the corresponding file channel FileChannel Fc = Rafile.GetChannel (); // Get the actual size of the file, so that Image to Shared Memory Int size = (int) fc.size (); // Get shared memory buffer, the shared memory read-only mappedbytebuffer mapbuf = fc.map (filechannel.map_ro, 0, size); // get one Located random access file object rafile = new randomaccessfile (filename, "rw"); // Get the corresponding file channel fc = rafile.getChannel (); // Get the actual size of the file so that the image is allowed to share memory Size = (int) fc.size (); // Get the shared memory buffer, the shared memory reads and writes mappbuf = fc.map (filechannel.map_rw, 0, size); // Get head message: Access permission mode = Mapbuf.Getint (); if multiple application images shared memory shared memory, this multiple applications share the same memory data. These applications can have equivalent access rights for files, and an application is updated to multiple applications for data. In order to prevent multiple applications from writing to the shared memory, the write operation flag can be added to the header information of the shared memory. The basic information of the shared memory is at least: intlength; // shared memory length. INT MODE; / / This shared memory current access mode. The header information of shared memory is a private information of the class. When multiple applications can perform a write operation with the same shared memory, when the write operation and end write operation are started, the following methods are required: public boolean startwrite () {ix (Mode == 0) {// Sign is 0, then write mode = 1; // Set the sign is 1, means that other applications cannot be written to share memory mapbuf.flip (); mapbuf.putint (Mode); / / Write, such as shared memory head information return true;} else {return false; // Indicates that there is already applying to write the shared memory, this application does not write the shared memory}} public boolean stopwrite () {MODE = 0; / / Release write permission mapbuf.flip (); mapbuf.putint (mode); // Write shared memory header information return true;} The class file MMap.java provided here has the basic interface of shared memory, readers can use it Category extends into a comprehensive class that you need. If the application exception abnormality is performed, the shared memory of the image file will no longer perform a write operation. In order to apply an abnormality, the write operation prohibits the logo automatically eliminates, and the application must be allowed to use the application.

转载请注明原文地址:https://www.9cbs.com/read-80062.html

New Post(0)