I have a class class with main thread
public class MainThread extends Thread
{
private final Socket s;
public MainThread(final Socket s)
{
this.s = s;
}
public void start()
{ // some logic
}
}
and class with pool of threads
public final class MainListener extends Thread
{
private Socket ss;
public MainListener(final int socket)
{
this.socket = socket;
this.pool = new ThreadPoolExecutor(
2,
4,
1000L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
public void start()
{
final ServerSocket ss = new ServerSocket(socket);
while(true)
{
final Socket s = ss.accept();
pool.execute(new MainThread(s));
}
}
MainThread is created but not started. It is ‘waiting’.
What is the problem? How start this thread after creation?
if I make constructor of MainThread like
public MainThread(final Socket s)
{
this.s = s;
start();
}
then all works normally. But it’s wrong
You’ve overridden
startinstead ofrun. To avoid making this mistake again, always useRunnable.