Queue characteristics: FIFO--advanced queue element first out queue.
It comes from the queues in our lives (the first line is done).
The following is a simple example to implement the loop queue operation.
1. Create a new Android app
2. Add a button to the interface
<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:paddi ngbottom= "@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 Andro Id:id= "@+id/start" android:layout_weight= "1" android:layout_width= "Wrap_content" Android: layout_height= "Wrap_content" android:text= "Start"/> <button android:id= "@+id/pause" android:layout_weight= "1" android:layOut_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Pause"/> <butt On android:id= "@+id/clear" android:layout_weight= "1" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "clear"/> </LinearLayout> <scrollv Iewandroid:layout_width= "Match_parent" android:layout_height= "match_parent" > <textview android:id= "@+id/Mo Nitordata "android:layout_width=" match_parent "android:layout_height=" Wrap_content "Android:scrollbar s= "Vertical" android:singleline= "false" android:textsize= "18SP" android:textstyle= "normal" and Roid:textcolor= "#000" android:text= "@string/hello_world"/></scrollview></linearlayout>
3. Processing effect
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;//thread sends data to the queue, Timer timed processing data 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;//Control Thread runs private boolean m_bisrunning = false;//queue operation public int m_nwt = 0;pub LIC int M_NRD = 0;public int m_ncnt=0;private final byte max_queue_size=20;private final byte max_element_size=6;public by Te m_ucmonitordata[][] = new Byte[max_queue_size][max_element_size];p rivateint nlength = 6;private byte txbuffer[] = new Byte[nlength];p rivate byte rxbuffer[] = new byte[nlength];//time Operation private int myear;private int mmonth;private int mday;private int mhour;private int mminute;private int msecond;public String m_sMonit ortime[] = new string[max_queue_size];//sync settings private Object Mutex = new Object ();//Timer Set Timer timer = new timer (); TimerTask task = new TimerTask () {@Overridepublic void run () {Message message = new Message (); message.what = Refresh;handler . sendMessage (message);}};/ /Threading @suppresslint ("Handlerleak") Handler Handler = new Handler () {public void Handlemessage (Message msg) {switch ( Msg.what) {case refresh:refreshdata (); break;default:break;}}};/ /thread 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 voidOnCreate (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;} Queue Time Final Calendar C = calendar.getinstance (); myear = C.get (calendar.year); mmonth = C.get (calendar.month) + 1;// The obtained month is 1 smaller than the actual month, so you need +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] = St r;//queue data 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];//time for (int j=0;j <max_element_size;j++) {strline = strline + Str.format ("%02x", M_ucmonitordata[index][j]);//Data}strline = strline + "\ n ";//newline 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, "reset queue");}}}
4. Operation effect
Start running
Pause Run
Clear
Start again
SOURCE download
Android Cycle Queue operation