TAILIEUCHUNG - programming LEGO MINDSTORMS phần 6

Các phương pháp quan trọng và thú vị nhất là hai phương pháp đồng bộ, getValue và setValue. Tính năng quan trọng nhất của phương pháp setValue là nó là rất ngắn, và do đó thực hiện rất nhanh. Nó sẽ được gọi bởi các LightReader | Programming for the leJOS Environment Chapter 6 213 Figure LineValueHolder Contains Main Synchronization Between Sensor Reading Code and Steering Code package linefollower public class LineValueHolder public int white public int black public int current public LineValueHolder public synchronized void setValue int value current value notify all waiting readers public synchronized int getValue throws InterruptedException wait until a new value is read return current public void setWhite int value white value public int getWhite return white public void setBlack int value black value public int getBlack return black The most important and interesting methods are the two synchronized methods getValue and setValue. The setValue method s most important feature is that it is very short and thus executes very fast. It will be called by the LightReader within its stateChanged 214 Chapter 6 Programming for the leJOS Environment method which in turn is called by the leJOS sensor reading thread. So the longer you tie up that sensor reading thread the more sensor readings you will miss. The two methods work in parallel as calls to getValue will block until another call has been made to reason this will work is that as mentioned previously sensor-readings will change Let s now take a look at our SensorListener which calls setValue see Figure . .V Figure LightReader Is Responsible for Delivering Sensor Readings to the LineValueHolder package linefollower import j import public class LightReader implements SensorListener LineValueHolder lvh public LightReader LineValueHolder lvh lvh public void stateChanged Sensor sensor int new_value int old_value new_value As you can see the stateChanged method is very short so it does not tie up the sensor reading thread as just mentioned. It simply calls the .

Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.