Reputation: 626
I'm working on a crawler which crawls the whole web 24x7 non stop in JAVA.
in order to increase the speed of crawling, I've implemented threads in it. But here is something I can't understand.
Consider the following code:
for(int i=0; i<no_of_threads; i++){
new Thread("" + i){
public void run()
{
try
{
System.out.println("Instance: " + getName() + " running");
getSeed();
Thread.sleep(1000);
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Thread could not start: "+getName());
}
}
}.start();
}
At first, the program shows following output:
Instance: 5 running
Instance: 6 running Instance: 4 running Instance: 3 running Instance: 2 running Instance: 1 running
Instance: 7 running Instance: 0 running
Instance: 8 running
Instance: 9 running
After this, it starts with normal execution and the program continuous to run for some time. And suddenly, I get:
Thread could not start: 6
and program continuous for some more time. and then,
Thread could not start: 9
and suddenly all threads are stopped.
The message "Thread could not start " is given only when exception is handled on thread start. But since, it already shows "Instance Running", it means the threads are already running.
I cannot understand why and how is this happening.
Upvotes: 0
Views: 107
Reputation: 178333
Your own message "Thread could not start" is incorrect. The thread has already started, but you just caught some kind of an Exception
within the Thread
itself. You are printing the exception's stack trace, but you didn't include it here. Examine the stack trace to determine what the real problem is. It's just not that the thread didn't start; there is some other problem within the thread.
Upvotes: 3