Solved

Killing a thread

Posted on 2004-03-29
13
297 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 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:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

762 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

22 Experts available now in Live!

Get 1:1 Help Now