File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Performance and the fly likes Killing threads issue Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Performance
Reply locked New topic
Author

Killing threads issue

Ismail
Greenhorn

Joined: May 16, 2002
Posts: 7
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

Joined: Mar 06, 2001
Posts: 13459

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


[ JavaRanch FAQ ][ Book Promotions ][ DbTamer ][ BumperStickers ][ JavaRanch Badges ]
 
 
subject: Killing threads issue
 
Threads others viewed
thread priority
Threads..join() method
Code confusion
Killing of Threads
Needs Explanation
IntelliJ Java IDE