Solved

Killing a thread

Posted on 2004-03-29
13
298 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
13 Comments
 
LVL 13

Accepted Solution

by:
Webstorm earned 64 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
 
LVL 5

Assisted Solution

by:twobitadder
twobitadder earned 62 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Assisted Solution

by:mmuruganandam
mmuruganandam earned 62 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 62 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now