Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Killing of Threads

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • 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
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using thread.interrupt to stop the thread and then thread.interrupted inside the thread to tell it is supposed to stop.
 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are other ways to cleanly stop a thread. Like changing your while (true) to a while(bool) and setting the variable bool to false whenever you have to stop the thread.
See also the forum
Thread.
W.
 
Ismail
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to stop the execution of a class which I cannot modify.
 
Jon Strayer
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If thread.interrupt doesn't stop the thread then the class is broken and you should beat up the vendor until they fix it.
 
Could you hold this puppy for a sec? I need to adjust this tiny ad:
Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF
htttp://www.e-iceblue.com/free-apis.html
    Bookmark Topic Watch Topic
  • New Topic