⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listenerthread.java

📁 专业汽车级嵌入式操作系统OSEK的源代码
💻 JAVA
字号:
package lejos.nxt;/** * Utility class for dispatching events to button, sensor and serial listeners. * * @author Paul Andrews */class ListenerThread extends Thread{  static ListenerThread singleton = new ListenerThread();  private static final int MAX_LISTENER_CALLERS = 7;  private static int [] masks;  private static ListenerCaller [] listenerCallers;  private static int numLC = 0;  int mask;  Poll poller = new Poll();     static ListenerThread get()  {      synchronized (singleton)      {          if (!singleton.isAlive())          {            masks = new int[MAX_LISTENER_CALLERS];            listenerCallers = new ListenerCaller[MAX_LISTENER_CALLERS];            singleton.setDaemon(true);            singleton.setPriority(Thread.MAX_PRIORITY);            singleton.start();          }      }            return singleton;  }  void addToMask(int mask, ListenerCaller lc)  {     int i;     this.mask |= mask;     for(i=0;i<numLC;i++) if (listenerCallers[i] == lc) break;     if (i == numLC) {       masks[numLC] = mask;       listenerCallers[numLC++] = lc;     }            // Interrupt the polling thread, not the current one!     interrupt();  }  void addButtonToMask(int id, ListenerCaller lc) {      addToMask(id << Poll.BUTTON_MASK_SHIFT, lc);  }    void addSensorToMask(int id, ListenerCaller lc) {      addToMask(1 << id, lc);  }  void addSerialToMask(ListenerCaller lc) {      addToMask(1 << Poll.SERIAL_SHIFT, lc);  }    public void run()  {      for (;;) {          try  {              int changed = poller.poll(mask, 0);              for(int i=0;i<numLC;i++)                 if ((changed & masks[i]) != 0)                   listenerCallers[i].callListeners();          } catch (InterruptedException ie) {          }      }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -