Code example for Semaphore

Methods: acquire, tryAcquire

0
	 
	private void wait(Semaphore sema)
	{ 
		try 
		{ 
			sema.acquire();
		} 
		catch(Exception e)
		{ 
		} 
	} 
 
	/*********************************************************************** 
	 Function    : Acquire() 
	 Input       : sensor input is ignored here! 
	 Output      : 
	 Return      : 
	 Description : acquires current sensors values and sends to 
	 		 	   QueryResolver component 
	***********************************************************************/ 
	public byte[] Acquire(String sensor, String query)
	{ 
		boolean read = false;
		int value = 0;
		byte [] readings = null;
		boolean value_waiting;
		 
		// anything there? 
		if (sensor_enable == true)
		{ 
			// see which sensors are requested 
			if (sensor.equals("Az") == true)
			{	 
				// is there a value waiting? 
				value_waiting = azimuth_semaphore.tryAcquire();
				 
				// if not, switch on listener 
				if (value_waiting == false)
				{ 
					// has Azimuth been started? 
					if (startedOrientation == false)
					{ 
						// send message to handler thread to start orientation 
				        Message msg = mHandler.obtainMessage(INIT_ORIENTATION);
				        mHandler.sendMessage(msg);	
					} 
	 
					wait(azimuth_semaphore); 
				} 
				if (azimuth != azimuth_old)
				{ 
					read = true;
					value = (int)(azimuth*10);
					azimuth_old = azimuth;
				} 
				 
				// now toggle event listener off, if necessary 
				if (value_waiting == false)
			        mHandler.sendMessage(mHandler.obtainMessage(CLOSE_ORIENTATION));						
			} 
			 
			if (read == false)
				if (sensor.equals("Pi") == true)
				{		 
					// is there a value waiting? 
					value_waiting = pitch_semaphore.tryAcquire();
					 
					// if not, switch on listener 
					if (value_waiting == false)
					{ 
						// has Pitch been started? 
						if (startedOrientation == false)
						{ 
							// send message to handler thread to start GPS 
					        Message msg = mHandler.obtainMessage(INIT_ORIENTATION);
					        mHandler.sendMessage(msg);	
						} 
	 
						wait(pitch_semaphore); 
					} 
					if (pitch != pitch_old)
					{ 
						read = true;
						value = (int)(pitch*10);
						pitch_old = pitch;
					} 
					 
					// now toggle event listener off, if necessary 
					if (value_waiting == false)
				        mHandler.sendMessage(mHandler.obtainMessage(CLOSE_ORIENTATION));						
				} 
 
			if (read == false)
				if (sensor.equals("Ro") == true)
				{		 
					// is there a value waiting? 
					value_waiting = roll_semaphore.tryAcquire();
					 
					// if not, switch on listener 
					if (value_waiting == false)
					{ 
						// has Roll been started?