public void contextInitialized(final ServletContextEvent event) {
try {
System.out.println("start thread");
Thread thread = new Thread(new SerialReader(event, serialPort,mode));
thread.start();
} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println("thread engaged");
}
이 코드를 실행하는 동안 오류가 없습니다. "thread engaged"는 절대로 인쇄되지 않습니다. 메인 스레드가 계속 실행되는 것을 막을 수있는 것은 무엇입니까?메인 스레드가 멈 춥니 다?
나는 완벽하게 작동
Thread thread = new Thread(new Runnable(){
public void run(){
try {
Thread.sleep(1000);
System.out.println("OUTPUT");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
});
로 교체하여이 문제를 테스트했습니다.
편집 : 생성자에서 일어나고있는 유일한입니다
private BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
public SerialReader(ServletContextEvent event, String port, int mode) throws Exception {
if (mode==1){
System.out.println("**Mode 1**");
} else {//mode is 1
}
event.getServletContext().setAttribute("serialPortData", queue);
}
EDIT2 : (서블릿 컨텍스트 리스너)이 무엇
private static final String SHUTDOWN_REQ = "SHUTDOWN";
public void attributeAdded(ServletContextAttributeEvent event) {
queue = (BlockingQueue<String>) event.getServletContext().getAttribute("serialPortData");
//we always get a null here on first try that's why I added null check
if (queue == null){
System.out.println("Queue is empty");
} else {
String item;
try {
//blocks while queue is empty
while ((item = queue.take()) != SHUTDOWN_REQ) {
System.out.println("*******WEB*******"+item+"*******");
//TODO Broadcast message to connected clients
}
} catch (InterruptedException e) {
System.out.println("queue error");
//e.printStackTrace();
}
}
}
'new SerialReader (event, serialPort, mode) ' – assylias
미안하지만 무슨 뜻인지 이해가 안 되니? – Thomas
아마도 이것이 실행이 멈춘 곳일 것입니다 ('SerialReader'의 생성자에서). – assylias