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
Solved

How can I stop a thread?

Posted on 2001-06-05
7
256 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

856 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