Code example for BlockingQueue

Methods: clear, isEmpty, put

0
 
	// Enqueue a command 
	public void send_message(String cmd) {
		try { 
			Log.d(TAG, "adding " + cmd);
			queue.put(cmd);
		} catch (InterruptedException e) {
			// This really shouldn't happen 
			e.printStackTrace();
		} 
	} 
 
	// Pause can be completed by either a resume or a flush. 
	public void send_stop() { 
		Log.d(TAG, "in send_stop()");
		send_pause(); 
		send_flush(); 
	} 
	 
	public void send_pause() { 
		if (!paused) {
			try { 
				writeLock.acquire();
				Log.d(TAG, "sending feedhold");
				write("!"); 
				ioLog.write("* ", "!\n");
				paused = true;
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block 
				e.printStackTrace();
			} 
		} 
	} 
	 
	public void send_flush() { 
			try { 
				if (!paused)
					writeLock.acquire();
				Log.d(TAG, "sending queue flush");
				write("%"); 
				ioLog.write("* ", "%\n");
				 
				Log.d(TAG, "permits: " + serialBufferAvail.availablePermits());
				int inuse = TINYG_BUFFER_SIZE - serialBufferAvail.availablePermits();
				if (inuse > 0)
					serialBufferAvail.release(inuse);
				if (!queue.isEmpty()) {
					queue.clear();
					flushed = true;
				} 
				paused = false;
				writeLock.release();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block 
				e.printStackTrace();
			} 
	} 
	 
	public void send_resume() { 
		if (paused) {
			Log.d(TAG, "Sending cycle start");
			write("~"); 
			ioLog.write("* ", "~\n");
			paused = false;
			writeLock.release();
		} 
	} 
	 
	public void send_reset() { 
		byte[] rst = {0x18};
		 
		Log.d(TAG, "in send_reset()");
		try { 
			writeLock.acquire();
			Log.d(TAG, "sending reset");
			ioLog.write("* ", "RESET\n");
			write(rst);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} 
 
		int inuse = TINYG_BUFFER_SIZE - serialBufferAvail.availablePermits();
		if (inuse > 0)
			serialBufferAvail.release(inuse);
		if (!queue.isEmpty()) {
			queue.clear();
			flushed = true;
		} 
		writeLock.release();
		refresh(); 
	}