[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Killing a thread

Posted on 2004-03-29
13
Medium Priority
?
309 Views
Last Modified: 2010-03-31
Can you please help me with a code for killing a thread.
0
Comment
Question by:krizmotlhala
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
13 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

649 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