//从队列中取出要处理的数据
  SendConditionData condition = queue.poll();
  if(condition != null) {
  log.info("begin to excute TaskSendSmsExcutor.excuteSendSMS");
  smsSendRecordService.sendSms(condition.getStatus().getValue(),
  condition.getSource().getValue(), condition.getFrom(), condition.getTo());
  } else {
  try {
  //将notify和wait方法包在同步块中
  synchronized (lock) {
  lock.wait();
  }
  } catch (InterruptedException e) {
  break;
  }
  }
  }
  }
  }
  }
  线程的使用示例,Session的保存
  import java.util.Map;
  import java.util.concurrent.ConcurrentHashMap;
  import javax.annotation.PostConstruct;
  import javax.annotation.PreDestroy;
  import org.springframework.stereotype.Component;
  import cn.ticai.ledcms.web.admin.data.UserSession;
  @Component
  public class SessionManager {
  /**
  * key : token, value : user session
  */
  private Map<String, UserSession> cache = new ConcurrentHashMap<String, UserSession>();
  private Thread daemonThread;
  public UserSession getSession(String token) {
  return cache.get(token);
  }
  public void setSession(String token, UserSession session) {
  cache.put(token, session);
  }
  @PostConstruct
  public void init() {
  //创建线程并且创建资源
  daemonThread = new Thread(new CleanupSessionTask());
  daemonThread.start();
  }
  @PreDestroy
  public void destroy() {
  daemonThread.interrupt();
  }
  class CleanupSessionTask implements Runnable {
  @Override
  public void run() {
  while(!Thread.interrupted()) {
  //从MAP中获取所有Session,并且遍历每个Session。如果时间超过二十分钟那么把该Session从MAP中移除
  UserSession[] sessions = cache.values().toArray(new UserSession[0]);
  for(int i = 0; i < sessions.length; i++) {
  long interval = System.currentTimeMillis() - sessions[i].getLastAccessTime().getTime();
  if(interval > 20 * 60 * 1000) {
  cache.remove(sessions[i].getToken());
  }
  }
  try {
  Thread.sleep(60*1000);
  } catch (InterruptedException e) {
  break;
  }
  }
  }
  }
  }