⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractmmf.java

📁 java的共享内存管理.基于MMF设计。封装了java.nio.MappedByteBuffer.在大流量实时业务系统时
💻 JAVA
字号:
/*
 * @(#)AbatractMMF.java	1.00 2007-12-5
 *
 * Copyright 2007 BCINFO. All Rights Reserved.
 * 
 * Programmer: Xuym.
 */
package com.bci.commons.mmf;

// import org.apache.log4j.Logger;

import java.util.Queue;
import java.util.Set;

import com.bci.commons.mmf.cell.AbstractCell;

/**
 * 共享内存管理类,继承自MemoryMappedFile,增加定长cell划分管理。
 * 
 * @author xuym
 * @version 1.00, 2007-12-5
 * @since JDK 1.5
 * @see java.nio.MappedByteBuffer
 * @see com.bci.commons.mmf.MemoryMappedFile
 */

public abstract class AbstractMMF extends MemoryMappedFile {
	/**
	 * Logger for this class
	 */
	// private static final Logger logger =
	// Logger.getLogger(SharedMemory.class);
	protected int cellLength;

	/**
	 * 空闲节点队列
	 */
	protected Queue<Integer> freeList = null;

	/**
	 * 占用节点队列
	 */
	protected Set<Integer> usedList = null;

	/**
	 * 初始化内存映象 应用程序需要在这里生成占用节点队列和空闲节点队列
	 * 
	 * @throws MemoryNotMappedException
	 *             If the file not mapped into memory
	 * @throws MemoryReadException
	 *             If some exception occurs when reading the memory.
	 */
	public abstract void initialize() throws MemoryNotMappedException,
			MemoryReadException;

	/**
	 * 在内存映象中申请一个空闲的消息单元
	 * 
	 * @throws MemoryNotMappedException
	 *             If the file not mapped into memory
	 * @throws MemoryReadException
	 *             If some exception occurs when reading the memory.
	 * @return the free cell's position. Returns -1 if request failure.
	 */
	public int request() throws MemoryNotMappedException, MemoryReadException {
		if (freeList == null)
			this.initialize();
		if (freeList.size() <= 0)
			return -1;
		int ret = freeList.poll();
		usedList.add(ret);
		return ret;
	}

	/**
	 * 在内存映象中释放指定位置的消息单元 这里假设每个cell的第一个字节为是否占用的标识
	 * 
	 * @param pos
	 *            the position of cell.
	 * @throws MemoryWriteException
	 * @throws MemoryNotMappedException
	 */
	public void release(int pos) throws MemoryNotMappedException,
			MemoryWriteException {
		this.write(pos, (byte) 0);
		freeList.offer(pos);
	}

	/**
	 * 取在内存中占用的节点集合
	 * 
	 * @return A set of the positions
	 */
	public Set<Integer> getUsedList() {
		if (usedList == null)
			this.initialize();
		return usedList;
	}

	/**
	 * 取在内存中空闲的节点队列
	 * 
	 * @return A queue of the positions.
	 */
	public Queue<Integer> getFreeList() {
		if (freeList == null)
			this.initialize();
		return freeList;
	}

	/**
	 * @return the cellLength
	 */
	public int getCellLength() {
		return cellLength;
	}

	/**
	 * @param cellLength
	 *            the cellLength to set
	 */
	public void setCellLength(int cellLength) {
		this.cellLength = cellLength;
	}

	/**
	 * 重新设置整个cell数据
	 * 
	 * @param pos
	 *            The dest position of the memory map.
	 * @return The old value at the given position.
	 * @throws MemoryNotMappedException
	 *             If the file not mapped into memory
	 * @throws MemoryReadException
	 *             If some exception occurs when reading the memory.
	 */
	public abstract AbstractCell dumpAndReset(int pos);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -