Solved

How can I stop a thread?

Posted on 2001-06-05
7
252 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
Comment Utility
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
Comment Utility
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
Comment Utility
You see, in Java2, Thread.stop() is deprecated and hence you need to make one yourself.


Master
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Expert Comment

by:shyamkumarreddy
Comment Utility
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
Comment Utility
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
Comment Utility
of course, the brute force way is

thread.destroy()
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility


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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now