消息环形队列

初始化队列

ArrayList<FrameBean> datas = new ArrayList<FrameBean>(m_nMaxDataNum);
ArrayList<MyInt> indexEmpty = new ArrayList<MyInt>(m_nMaxDataNum);
ArrayList<MyInt> indexs = new ArrayList<MyInt>(0);
Lock lock = new ReentrantLock();

public YuvDataModel() {
Init();
}



void Init() {
for (int i = 0; i < m_nMaxDataNum; i++) {
indexEmpty.add(new MyInt(i));
datas.add(new FrameBean());
}
}



获取可用的空元素,以便从datas中获取数据

MyInt GetEmptyIndex() {
MyInt nIndex = null;
Lock();
// 鍙栧嚭鏈夋晥鐨刬d
if (indexEmpty.size() > 0) {
MyInt nTemp = indexEmpty.get(0);
indexEmpty.remove(0);
if (IsIndexValid(nTemp.m_value) == false) {
System.out.println(String
.format("Error: Find Invalid empty index!"));
}
{
nIndex = nTemp;
// break;
}
}


unlock();
return nIndex;
}

获取indexs 数值,将对应此数据下标的FrameBean元素提供出去
MyInt GetDataIndex() {
MyInt nIndex = null;
try {
Lock();

if (indexs.size() > 0) {
MyInt nTemp = indexs.get(0);
indexs.remove(0);
if (IsIndexValid(nTemp.m_value) == false) {
System.out.println(String
.format("Error: Find Invalid data index!"));
}
{
nIndex = nTemp;
//break;
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
} finally {
unlock();
}
return nIndex;
}

void AddDataIndex(MyInt nIndex) {
try {
Lock();
indexs.add(nIndex);
} catch (Exception e) {


} finally {
unlock();
}
}


void AddEmptyIndex(MyInt nIndex) {
try {
Lock();
indexEmpty.add(nIndex);
} catch (Exception e) {


e.printStackTrace();
System.out.println(e.getMessage());
} finally {
unlock();
}
}

获取空元素队列indexEmpty 中的数值将对应此数据的下标的数据取出来放入一个FrameBean对象中,此时indexEmpty需要移除一个元素,indexs中添加一个元素使用AddDataIndex()方法,接着通过pop方法将取到的FrameBean对象提供出去,



public void push() 
{
/*
int size=data.length;
if (size < 1) {
return;
}
*/
MyInt nIndex = GetEmptyIndex();
if (nIndex == null) {
return;
}


if (IsIndexValid(nIndex.m_value) == true) {
FrameBean dd = datas.get(nIndex.m_value);

if (dd.databyte == null ) {
dd.databyte = new byte[m_YuvLen];
}

System.arraycopy(m_YuvDirectBuf.array(), 0, dd.databyte, 0, m_YuvLen);
dd.width = m_width;
dd.height = m_height;
}


AddDataIndex(nIndex);
}


pop方法中indexEmpty 需要添加一个元素,indexs中移除一个元素

public int pop(FrameBean data) {
int ret = -1;
MyInt nIndex = GetDataIndex();
if (nIndex == null) {
return -1;
}


if (IsIndexValid(nIndex.m_value) == true) {
FrameBean dd = datas.get(nIndex.m_value);
if (dd.databyte == null || dd.databyte.length < 1) {
} else {
if (data.databyte == null
|| data.databyte.length != dd.databyte.length) {
data.databyte = new byte[dd.databyte.length];
}
System.arraycopy(dd.databyte, 0, data.databyte, 0,
dd.databyte.length);
data.width = dd.width;
data.height = dd.height;
ret = dd.databyte.length;


}
}
AddEmptyIndex(nIndex);
return ret;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值