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

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

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


1.新建Android应用程序



2.界面上添加按钮

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity"    android:orientation="vertical" >        <LinearLayout         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <Button             android:id="@+id/start"            android:layout_weight="1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="开始"/>        <Button             android:id="@+id/pause"            android:layout_weight="1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="暂停"/>        <Button             android:id="@+id/clear"            android:layout_weight="1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="清除"/>    </LinearLayout>    <ScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent">    <TextView         android:id="@+id/MonitorData"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:scrollbars="vertical"        android:singleLine="false"        android:textSize="18sp"        android:textStyle="normal"        android:textColor="#000"android:text="@string/hello_world"/></ScrollView></LinearLayout>


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(){@Overridepublic 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(){@Overridepublic 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);}}});@Overrideprotected 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);}@Overrideprotected 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() {@Overridepublic 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,所以需要+1mDay = 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<nLength;i++){m_ucMonitorData[m_nWt][i] = TxBuffer[i];}m_nWt++;if(m_nWt >= 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_ELEMENT_SIZE;i++){RxBuffer[i] = m_ucMonitorData[m_nRd][i];}m_nRd++;if(m_nRd >= 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<m_nCnt;i++){strline = strline + m_sMonitorTime[index];//时间for(int j=0;j<MAX_ELEMENT_SIZE;j++){strline = strline + str.format("%02X ",m_ucMonitorData[index][j]);//数据}strline = strline + "\n";//换行index++;if(index >= MAX_QUEUE_SIZE){index = 0;}}return strline;}}public void ResetQueue(){synchronized (Mutex) {m_nCnt = 0;m_nWt = 0;m_nRd = 0;Log.d(TAG, "重置队列");}}}


4.运行效果

开始运行


暂停运行


清除



重新开始



源码下载


更多相关文章

  1. 浅析Android中的消息机制-解决:Only the original thread that cr
  2. Android异步消息机制之Handler
  3. Android的Handler机制详解3_Looper.looper()不会卡死主线程
  4. Android之Handler用法总结
  5. Android开发之消息处理机制(一)——Handler
  6. Android异步加载图像小结 (含线程池,缓存方法)
  7. android 面试题集
  8. android 电容屏(二):驱动调试之基本概念篇
  9. [Innost]Android深入浅出之Binder机制

随机推荐

  1. android p make 编译lib静态库报错
  2. Android 如何实现竖排文字显示?
  3. Android调用系统相机拍摄视频以及获取缩
  4. android makefile 常用宏名称和内置变量
  5. Android判断一个Service是否运行
  6. Android學習筆記整理 2011.02.15 1
  7. WebView Apps
  8. android 编译错误
  9. android跳转其他app地图高德、百度、腾讯
  10. listView布满小图