我有以下课程:
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerConfig;
import javax.ejb.TimerService;
import javax.inject.Inject;
import com.mysite.Config;
@Startup
@Singleton
public class Scheduler {
@Resource
private TimerService timerService;
@Inject @Config
private Logger log;
@Inject @Config
private Integer delay;
@Inject @Config
private Integer interval;
@Inject @Config
private Boolean enabled;
@PostConstruct
public void initTimer() {
if (enabled) {
TimerConfig tc = new TimerConfig();
tc.setPersistent(false);
timerService.createIntervalTimer(delay, interval, tc);
}
}
@Timeout
public void timeout(Timer timer) {
// do something
} …Run Code Online (Sandbox Code Playgroud) 我正在写一个基于ncurses的聊天程序.起初,我只编写网络内容(没有ncurses),一切正常,但添加图形后,我无法使客户端应用程序正常工作.
主要问题是同时从stdin和socket读取.在ncurses-less版本中,我使用了pthread,它就像魅力一样.唉,似乎pthread和ncurses不能很好地结合在一起,所以我必须找到另一个解决方案.我认为select()会这样做,但它仍然只从stdin读取并完全忽略套接字.
这是整个代码:代码
有趣的是:
char message[1024];
fd_set master;
fd_set read_fds;
FD_ZERO(&master);
FD_ZERO(&read_fds);
FD_SET(0,&master);
FD_SET(s,&master); // s is a socket descriptor
while(true){
read_fds = master;
if (select(2,&read_fds,NULL,NULL,NULL) == -1){
perror("select:");
exit(1);
}
// if there are any data ready to read from the socket
if (FD_ISSET(s, &read_fds)){
n = read(s,buf,max);
buf[n]=0;
if(n<0)
{
printf("Blad odczytu z gniazdka");
exit(1);
}
mvwprintw(output_window,1,1,"%s\n",buf);
}
// if there is something in stdin
if (FD_ISSET(0, &read_fds)){
getstr(message);
move(CURS_Y++,CURS_X);
if (CURS_Y == LINES-2){
CURS_Y = 1; …Run Code Online (Sandbox Code Playgroud)