Solved

How can I stop a thread?

Posted on 2001-06-05
7
255 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Checkbox and ListView in Android Layout 4 65
java 8 lambda expresssions exception handling 3 88
Opening PDF on button click and fill new document 2 36
arguments to jar 5 26
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

776 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