I came across one question when I gone through a JMS book. Below is the code. My question is related to thread so I removed the unnecessary JMS code.
public class MessageConsumer implements MessageListener{
public MessageConsumer(){ //Constructor
//configure JMS Connections
}
@Override
public void onMessage(Message message) {
//receive message
}
public static void main(String[] args) {
new Thread(){
@Override
public void run() {
new MessageConsumer();
}
}.start();
}
}
In the above code why the author is invoking the constructor as a new thread. I tried invoking constructor as below and it gives same result
public static void main(String[] args) {
new MessageConsumer();
}
So is there any difference in the above two ways. I know creating a new Thread() will create a separate new thread. However, in this simple example for JMS, do I need to invoke the constructor as separate thread?