Android之循环队列操作

队列特性:先进先出(FIFO)——先进队列的元素先出队列。

来源于我们生活中的队列(先排队的先办完事)。

下面以一个简单的例子实现循环队列的操作。


1.新建Android应用程序

Android之循环队列操作_第1张图片


2.界面上添加按钮


    
    
        
Android之循环队列操作_第2张图片


3.处理效果

package com.sl.queuedemo;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

//线程发送数据入队,定时器定时处理数据
public class MainActivity extends Activity
{
	private static final String TAG = "QueueDemo";
	
	static final int REFRESH = 0;
	private TextView mTextView;
		
	public Button m_btnStart = null;
	public Button m_btnPause = null;
	public Button m_btnClear = null;
	private boolean m_bIsReading = false;

	//控制线程运行
	private boolean m_bIsRunning = false;
	
	//队列操作
    public int m_nWt = 0;
	public int m_nRd = 0;
	public int m_nCnt=0;
	private final byte MAX_QUEUE_SIZE	=	20;
	private final byte MAX_ELEMENT_SIZE	=	6;
	public byte m_ucMonitorData[][] = new byte[MAX_QUEUE_SIZE][MAX_ELEMENT_SIZE];
	
	private int nLength = 6;
	private byte TxBuffer[] = new byte[nLength];
	private byte RxBuffer[] = new byte[nLength];
	
	//时间操作
	private int mYear;
	private int mMonth;
	private int mDay;
	private int mHour;
	private int mMinute;
	private int mSecond;
	public String m_sMonitorTime[] = new String[MAX_QUEUE_SIZE];
	
	//同步设置
	private Object Mutex = new Object();
	
	//定时器设置
	Timer timer = new Timer();
	TimerTask task = new TimerTask()
	{
		@Override
		public void run()
		{
			Message message = new Message();
			message.what = REFRESH;
			handler.sendMessage(message);
		}
	};
	
	//线程处理
	@SuppressLint("HandlerLeak")
	Handler handler = new Handler()
	{
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
			case REFRESH:
				RefreshData();
				break;
			default:
				break;
			}
		}
	};
	
	//线程
	public Thread myThread = new Thread(new Runnable()
	{
		@Override
		public void run()
		{
			while(m_bIsRunning)
			{
				TxBuffer[0]++;
				TxBuffer[1]++;TxBuffer[1]++;
				TxBuffer[2]++;
				TxBuffer[3]++;
				TxBuffer[4]++;TxBuffer[4]++;
				TxBuffer[5]++;TxBuffer[5]++;TxBuffer[5]++;
				InQueue(TxBuffer, nLength);
				
				SystemClock.sleep(1000);
			}
		}
	});
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mTextView = (TextView)findViewById(R.id.MonitorData);
		
		m_btnStart = (Button)findViewById(R.id.start);
		m_btnPause = (Button)findViewById(R.id.pause);
		m_btnClear = (Button)findViewById(R.id.clear);
		m_btnStart.setOnClickListener(listener);
		m_btnPause.setOnClickListener(listener);
		m_btnClear.setOnClickListener(listener);
		
        m_bIsRunning = true;
        myThread.start();
		timer.schedule(task,1000,1000);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		m_bIsRunning = false;
		timer.cancel();
		timer.purge();
	}
	
	public void RefreshData()
	{		
		if(m_bIsReading)
		{
			String str = GetOutQueueString();
			mTextView.setText(str);
		}
	}
	
	OnClickListener listener = new View.OnClickListener() 
	{		
		@Override
		public void onClick(View v) 
		{
			switch(v.getId())
			{
			case R.id.start:
				m_bIsReading = true;
				RefreshData();
				break;
			case R.id.pause:
				m_bIsReading = false;
				break;
			case R.id.clear:
				ResetQueue();
				RefreshData();
				break;
			default:
				break;
			}
		}
	};
	
	public void InQueue(final byte TxBuffer[],final int nLength)
	{
		synchronized (Mutex) 
		{
			if(nLength<=0)
			{
				return;
			}
			
			//入队时间
			final Calendar c = Calendar.getInstance();
			mYear = c.get(Calendar.YEAR);
			mMonth = c.get(Calendar.MONTH) + 1;//获取的月份比实际月份小1,所以需要+1
			mDay = c.get(Calendar.DAY_OF_MONTH);
			mHour = c.get(Calendar.HOUR_OF_DAY);
			mMinute = c.get(Calendar.MINUTE);
			mSecond = c.get(Calendar.SECOND);

			String year = "0" + mYear;
			year = year.substring(year.length()-2, year.length());
			String month = "0" + mMonth;
			month = month.substring(month.length()-2, month.length());
			String day = "0" + mDay;
			day = day.substring(day.length()-2, day.length());
			String hour = "0" + mHour;
			hour = hour.substring(hour.length()-2, hour.length());
			String minute = "0" + mMinute;
			minute = minute.substring(minute.length()-2, minute.length());
			String second = "0" + mSecond;
			second = second.substring(second.length()-2, second.length());
			
			String str = year + "." + month + "." + day + " " + hour + ":" + minute + ":" + second + " -- ";
			m_sMonitorTime[m_nWt] = str;
			
			//入队数据
			for(int i =0;i= MAX_QUEUE_SIZE)
			{
				m_nWt = 0;
			}
			m_nCnt++;
			if(m_nCnt > MAX_QUEUE_SIZE)
			{
				m_nCnt = MAX_QUEUE_SIZE;
				m_nRd++;
				if(m_nRd >= MAX_QUEUE_SIZE)
				{
					m_nRd = 0;
				}
			}
		}
	}
	
	public boolean OutQueue(byte RxBuffer[])
	{
		synchronized (Mutex) 
		{
			if(m_nCnt <= 0)
			{
				return false;
			}
			for(int i=0;i= MAX_QUEUE_SIZE)
			{
				m_nRd = 0;
			}
			m_nCnt--;
			return true;
		}
	}
	
	public String GetOutQueueString()
	{
		synchronized (Mutex) 
		{
			String strline = "";
			String str = "";
			int index = m_nRd;
			for(int i=0;i= MAX_QUEUE_SIZE)
				{
					index = 0;
				}
			}
			return strline;
		}
	}
	
	public void ResetQueue()
	{
		synchronized (Mutex) 
		{
			m_nCnt = 0;
			m_nWt = 0;
			m_nRd = 0;
			Log.d(TAG, "重置队列");
		}
	}
}
Android之循环队列操作_第3张图片


4.运行效果

开始运行

Android之循环队列操作_第4张图片 Android之循环队列操作_第5张图片


暂停运行

Android之循环队列操作_第6张图片


清除

Android之循环队列操作_第7张图片

重新开始

Android之循环队列操作_第8张图片 Android之循环队列操作_第9张图片


源码下载


你可能感兴趣的:(Android开发)