Solved

How can I stop a thread?

Posted on 2001-06-05
7
258 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
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 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

696 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