Solved

How can I stop a thread?

Posted on 2001-06-05
7
257 Views
Last Modified: 2010-03-31
hi every body,
I just want to stop a thread.I have a thread class and i'm starting it from a servlet depending upon a request parameter.for eg i want to start the servlet if the request is "ok" and if the request parameter is "cancel" then i want to stop the executed thread if any..

for eg
my thread prog
public class TestThread extends Thread implements Serializable
{

    public void run()
    {
        try
        {
        for(int i=0; i<1000; i++)
        {
            System.out.println("Number is="+ i);
            this.sleep(1000);
        }
        }
        catch(Exception e)
        {
         
            e.printStackTrace();
        }
    }

}



my servlet

public class ThreadTestServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
    {
        try
        {
            TestThread tt = new TestThread();
        if(req.getParameter("name").equals("ok"))
            {
              tt.start();
            }
        else if(req.getParameter("name").equals("cancel"))
{
//here I want to stop the executed thread

}

         }
catch(Exception e){}
}



How is it possible

Please help me out

Thanks in advance
Kumar
0
Comment
Question by:harish_kg
[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
7 Comments
 
LVL 2

Expert Comment

by:mgaurav
ID: 6157621
In Java1.2 you don't have suspend() and stop() (Deprecated).

 Put a variable
   boolean stop = false;

Now do the following:
  threadVar.myStop();

public void myStop()
{
  stop = true;
  notify();
}

public void run()
{
  while(!stop)
  {
   continue doing the process.
  }

::::::::OR THIS METHOD:::::

  do the work and then wait

  while(!stop)
  {
   wait();
  }
}


Hope it helps!!


Master

0
 
LVL 2

Expert Comment

by:bagi112599
ID: 6157659
Harish,
Don't you need to know which thread to stop?
Servlet is multiuser environment, and I guess in your application you'll be starting a thread per each client?
And you will need to stop that thread that is started by this client?
Then you will need to have an mechanism that keeps track of each thread.
Let us know what your spec is.
0
 
LVL 2

Accepted Solution

by:
mgaurav earned 50 total points
ID: 6157669
You see, in Java2, Thread.stop() is deprecated and hence you need to make one yourself.


Master
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6158535
Hi Harish

U can stop thread but it is deprecated in Java2.
It is not advisible to stop also since other instance may be using this thread.

Still u want to stop good
Then make that thread to daemon thread. And call garbage collection using Runtime.gc() then ur thread will be cleared from the memory
set it as daemon thread

Cheers
Shyam
0
 
LVL 2

Expert Comment

by:mariuso
ID: 6159014
You have to doit in this way:

public class TestThread extends Thread implements Serializable
{    
    public boolean isStopped = false;

    public void run()
    {
        try
        {
        for(int i=0; i<1000 && !isStopped; i++)
        {
            System.out.println("Number is="+ i);
            this.sleep(1000);
        }
        }
        catch(Exception e)
        {
         
            e.printStackTrace();
        }
    }

}



my servlet

public class ThreadTestServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
    {
        try
        {
            TestThread tt = new TestThread();
        if(req.getParameter("name").equals("ok"))
            {
              tt.start();
            }
        else if(req.getParameter("name").equals("cancel"))
{
//here you want to stop the executed thread
     tt.isStopped = true; // will stop the thrad in a safe way.

}

         }
catch(Exception e){}
}
0
 
LVL 1

Expert Comment

by:jimsims
ID: 6178532
of course, the brute force way is

thread.destroy()
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 8899007


No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:


--  Points to mgaurav


Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java string replace 11 63
jboss wildfly 10.1 10 437
Convert from a json string array to a Java object 3 65
jmss example java 2 33
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

726 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