Android 线程处理

追风赶月莫停留,平芜尽处是春山。这篇文章主要讲述Android 线程处理相关的知识,希望能为你提供帮助。
    synchronized处理线程wait() 和notifyAll() 时,同步代码块中不要包含Thread.sleep(5)语句:
 

1 package com.csizg.test; 2 3 4 import java.util.ArrayList; 5 import java.util.List; 6 7 /** 8* 与芯片交互线程 9*/ 10 public class SpiQueue implements Runnable { 11 12private static final String TAG = SpiQueue.class.getSimpleName(); 13 14private static final byte INIT = 0; // 初始化 15private static final byte RUNNING = 1; // 执行中 16private static final byte WITING = 2; // 暂停等待中 17private static final byte DONE = 3; // 结束 18 19private byte state = INIT; // 执行状态 20 21private static SpiQueue spiQueue = null; 22private boolean isTalking = false; 23 24private SpiQueue() { 25 26} 27 28public static SpiQueue getSpiQueue() { 29if (spiQueue == null) { 30spiQueue = new SpiQueue(); 31} 32return spiQueue; 33} 34 35private Thread thread; 36private List< SpiTask> tasks = new ArrayList< > (); 37 38@Override 39public void run() { 40state = RUNNING; 41while (state != DONE) { 42 43SpiTask currentTask = getNextTask(); 44if (currentTask == null) { 45if (isTalking) { 46System.out.println(TAG + "run" + "no task todo begin to sleep"); 47try { 48Thread.sleep(5); 49} catch (InterruptedException e) { 50e.printStackTrace(); 51} 52continue; 53} else { 54System.out.println(TAG + "run" + "no task todo begin to waiting"); 55synchronized (this) { 56try { 57state = WITING; 58wait(); 59} catch (InterruptedException e) { 60e.printStackTrace(); 61} 62} 63} 64} else { 65try { 66Thread.sleep(10); 67} catch (InterruptedException e) { 68e.printStackTrace(); 69} 70System.out.println(TAG + "run" + "currentTask = " + currentTask.toString()); 71} 72 73} 74 75} 76 77private synchronized SpiTask getNextTask() { 78System.out.println(TAG + "getNextTask" + "tasks.size() = " + tasks.size()); 79if (tasks.size() > 0) { 80SpiTask task = tasks.remove(0); 81return task; 82} 83return null; 84} 85 86public synchronized void addTask(SpiTask task) { 87System.out.println(TAG + "addTask" + "tasks.size() = " + tasks.size() + ", new task : " + task.getCode()); 88if (state == DONE) { 89System.out.println(TAG + "addTask" + "Thread is DONE : task : " + task.getCode()); 90return; 91} 92tasks.add(task); 93if (state != RUNNING) { 94synchronized (this) { 95state = RUNNING; 96System.out.println(TAG + "addTask notifyAll "); 97notifyAll(); 98} 99} 100} 101 102public synchronized void start() { 103System.out.println(TAG + "start" + "state = " + state); 104if (state == RUNNING || state == WITING) { 105return; 106} 107state = INIT; 108// 运行本线程 109tasks.clear(); 110if (thread != null) { 111thread.interrupt(); 112} 113thread = new Thread(this); 114thread.start(); 115} 116 117 118public synchronized void stop() { 119state = DONE; 120tasks.clear(); 121isTalking = false; 122synchronized (this) { 123notifyAll(); 124} 125} 126 127public void setTalking(boolean isTalking) { 128this.isTalking = isTalking; 129} 130 }

结果花费1.6s;
 
 
1 package com.csizg.test; 2 3 4 import java.util.ArrayList; 5 import java.util.List; 6 7 /** 8* 与芯片交互线程 9*/ 10 public class SpiQueue implements Runnable { 11 12private static final String TAG = SpiQueue.class.getSimpleName(); 13 14private static final byte INIT = 0; // 初始化 15private static final byte RUNNING = 1; // 执行中 16private static final byte WITING = 2; // 暂停等待中 17private static final byte DONE = 3; // 结束 18 19private byte state = INIT; // 执行状态 20 21private static SpiQueue spiQueue = null; 22private boolean isTalking = false; 23 24private SpiQueue() { 25 26} 27 28public static SpiQueue getSpiQueue() { 29if (spiQueue == null) { 30spiQueue = new SpiQueue(); 31} 32return spiQueue; 33} 34 35private Thread thread; 36private List< SpiTask> tasks = new ArrayList< > (); 37 38@Override 39public void run() { 40state = RUNNING; 41while (state != DONE) { 42synchronized (this) { 43SpiTask currentTask = getNextTask(); 44if (currentTask == null) { 45if (isTalking) { 46System.out.println(TAG + "run" + "no task todo begin to sleep"); 47try { 48Thread.sleep(5); 49} catch (InterruptedException e) { 50e.printStackTrace(); 51} 52continue; 53} else { 54System.out.println(TAG + "run" + "no task todo begin to waiting"); 55try { 56state = WITING; 57wait(); 58} catch (InterruptedException e) { 59e.printStackTrace(); 60} 61} 62} else { 63try { 64Thread.sleep(10); 65} catch (InterruptedException e) { 66e.printStackTrace(); 67} 68System.out.println(TAG + "run" + "currentTask = " + currentTask.toString()); 69} 70} 71} 72 73} 74 75private synchronized SpiTask getNextTask() { 76System.out.println(TAG + "getNextTask" + "tasks.size() = " + tasks.size()); 77if (tasks.size() > 0) { 78SpiTask task = tasks.remove(0); 79return task; 80} 81return null; 82} 83 84public synchronized void addTask(SpiTask task) { 85System.out.println(TAG + "addTask" + "tasks.size() = " + tasks.size() + ", new task : " + task.getCode()); 86if (state == DONE) { 87System.out.println(TAG + "addTask" + "Thread is DONE : task : " + task.getCode()); 88return; 89} 90tasks.add(task); 91if (state != RUNNING) { 92synchronized (this) { 93state = RUNNING; 94System.out.println(TAG + "addTask notifyAll "); 95notifyAll(); 96} 97} 98} 99 100public synchronized void start() { 101System.out.println(TAG + "start" + "state = " + state); 102if (state == RUNNING || state == WITING) { 103return; 104} 105state = INIT; 106// 运行本线程 107tasks.clear(); 108if (thread != null) { 109thread.interrupt(); 110} 111thread = new Thread(this); 112thread.start(); 113} 114 115 116public synchronized void stop() { 117state = DONE; 118tasks.clear(); 119isTalking = false; 120synchronized (this) { 121notifyAll(); 122} 123} 124 125public void setTalking(boolean isTalking) { 126this.isTalking = isTalking; 127} 128 }

花费时间11.2s;
 
 
测试调用代码:
1 public void test() { 2SpiTask spiTask = null; 3for (int i = 0; i < 30; i++) { 4spiTask = new SpiTask("Thread --- " + i, "" + i); 5if (i > 6) { 6SpiQueue.getSpiQueue().setTalking(true); 7} 8SpiQueue.getSpiQueue().addTask(spiTask); 9try { 10Thread.sleep(20); 11} catch (InterruptedException e) { 12e.printStackTrace(); 13} 14} 15SpiQueue.getSpiQueue().setTalking(false); 16}

 
 
【Android 线程处理】当synchronized同步代码块中去掉Thread.sleep(5)时,结果一致,都是1.6s左右。

    推荐阅读