Hi,
I know that killing of threads with stop() method of
Thread class is deprecated in
JAVA. But still I need this for deliberately killing a thread because I don't have any database activities in my program.
Here is a sample program I wrote for killing threads. But I found that the program hangs several times during execution.
Can you identify what is the problem in this program.
Here is the class which starts a thread :
public class ThreadStopTest
{
public static void main(
String args[])
{
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
SampleThread st = new SampleThread();
st.setPriority(Thread.MIN_PRIORITY);
st.start();
long starttime = System.currentTimeMillis();
try
{
Thread.currentThread().sleep(100);
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
System.out.println(" Time waited :: " + (System.currentTimeMillis() - starttime));
System.out.println("Thread started ");
st.stop();
System.out.println("Thread stop called");
System.out.println("Checking activity of thread :: " + st.isAlive());
}
}
Here is the class which is the thread.
public class SampleThread extends Thread
{
SampleThread()
{
}
public void run()
{
int i = 0;
infiniteloop:
while (true)
{
if (i == 1000)
break infiniteloop;
System.out.println("Current i is :: " + i);
i++;
System.out.println("This thread terminates when the count reaches 10000000");
}
}
}
It is very important that I should have a solution for this because we are using this in a project.
Thanks,
Ismail