• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

Killing a thread

Can you please help me with a code for killing a thread.
0
krizmotlhala
Asked:
krizmotlhala
  • 4
  • 3
  • 2
  • +2
4 Solutions
 
WebstormCommented:
Hi krizmotlhala,


thread.interrupt();


0
 
WebstormCommented:
If the thread is created by your Java program.
0
 
CEHJCommented:
What is the thread doing when you want to stop it?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
twobitadderCommented:
You can provide a method that interrupts it and lets it fall through its code or a method that changes a variable to let it fall out of a while loop.

eg.
volatile boolean finished  = false;

public void run()
{
  while(!finished)
  {
    doStuff
  }
}

public void endThread()
{
  finished = true;
}
0
 
objectsCommented:
a thread is killed when its run() method completes.
the example posted by  twobitadder shows a method for achieving that.
0
 
mmuruganandamCommented:
>> thread.interrupt();

This won't alone help you in killing the thread.


run()
{
   try
    {
             ---------
             ---------
    }
    catch(InterruptedException e)
    {
        Thread.currentThread().interrupt(); // this is a must need to kill the thread properly.
    }
}
0
 
objectsCommented:
0
 
WebstormCommented:
>> This won't alone help you in killing the thread.
You code is totally illogic, mmuruganandam. Why do you interrupt the thread when the thread is already interrupted ??

Don't say that thread.interrupt(); won't help until you test it.


0
 
mmuruganandamCommented:
If that code is ilogic, the orielly is totally wrong.  Some of the projects that are running with this code not running at all.

Professionalism is much more important in this kind of forums.
Read the words that are used by me and you.

0
 
WebstormCommented:
>> Professionalism is much more important in this kind of forums.
I'm sorry if you misundertood my comment, i hope you would excuse me.
I only wanted to know why.

0
 
mmuruganandamCommented:
no harms webstrom!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now