?
Solved

Killing a thread

Posted on 2004-03-29
13
Medium Priority
?
311 Views
Last Modified: 2010-03-31
Can you please help me with a code for killing a thread.
0
Comment
Question by:krizmotlhala
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 13

Accepted Solution

by:
Webstorm earned 256 total points
ID: 10708011
Hi krizmotlhala,


thread.interrupt();


0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10708018
If the thread is created by your Java program.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10708022
What is the thread doing when you want to stop it?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Assisted Solution

by:twobitadder
twobitadder earned 248 total points
ID: 10708058
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
 
LVL 92

Expert Comment

by:objects
ID: 10708377
a thread is killed when its run() method completes.
the example posted by  twobitadder shows a method for achieving that.
0
 
LVL 9

Assisted Solution

by:mmuruganandam
mmuruganandam earned 248 total points
ID: 10710460
>> 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
 
LVL 92

Assisted Solution

by:objects
objects earned 248 total points
ID: 10710483
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10711821
>> 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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10711844
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
 
LVL 13

Expert Comment

by:Webstorm
ID: 10722172
>> 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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10722182
no harms webstrom!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question