Bookmark Topic Watch Topic
  • New Topic

Killing threads issue  RSS feed

 
Ismail
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
You have asked the same question in the Java in General (advanced) forum here and it has been answered.
I'm going to close this thread and direct anyone who wants to contribute to the other thread.
Please don't post the same question in multiple forums, it promotes duplicate conversations and wastes peoples time
Dave
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!