I try to write the code the receive the socket data by create a thread.But my workmate say that I should use ScheduledExecutorService because the thread may be hang or crash.
//this is my code 1
ExecutorService executorService= Executors.newSingleThreadExecutor();
executorService.execute(new receiveThread());
//this is my code 2
new receiveThread().run();
//the thread:
class receiveThread implements Runnable{
@Override
public void run() {
while (true) {
try {
dealWithReceiveData()
} catch (Exception e) {
logger().error(logMsg() + ExceptionUtils.getStackTrace(e));
}
}
}
}
And my workmate's suggestion:
private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(10);
scheduler.scheduleAtFixedRate(new receiveThread(),0,30, TimeUnit.SECONDS);
Is my workmate's suggestion right?I think the two way is the same effect because I have add try catch