?
Solved

Killing a thread

Posted on 2004-03-29
13
Medium Priority
?
305 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month12 days, 10 hours left to enroll

777 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