Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is the best way to stop a thread?

Status
Not open for further replies.

bledazemi

Programmer
Jul 14, 2005
93
GB
I am new to Java and am currently implementing a project which will have many threads running. I would really appreciate if someone could help me find the answer to the following:

1. How many threads can i run at the same time in Java? What is the maximum ?


2. Why are Thread.suspend() and Thread.stop() methods deprecated? Or more to the point if the stop() method is deprecated what is the non-deprecated way of terminating a thread?

Thank you in advance. Your help is much appreciated.
 
2. create a boolean variable, let's call it running, and set it to true on start, and run until the job is done (the thread might decide itself) or set it from a controlling instance to false, and run until it is set to false.

1. Multithreading isn't a good point to start with java, but to learn rapidly (perhaps you know OO-Programming from other languages) it would be helpful, if you write a program to answer your question yourself.
If that's too hard - we can help you in finding out what's going wrong.

I could tell you a number (from my system and java-version), but what would you learn this way?

seeking a job as java-programmer in Berlin:
 
The normal way to stop a thread is to implement Thread.interrupt() in your thread class, and then call thread.interrupt() when you wish it to stop.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Thanks a lot for your help guys

Thanks to stefan wagner for his suggestion of using a boolean variable to control the loop of my threads.

Thanks to timw for his link, it was certainly useful.

Sedj i am not sure how the thread.interrupt() would do the job and to be honest i dont like to the name of the method. It sounds to me like this method i would reserve to force a thread to stop. But in any case thank you very much to all.

I followed your suggestions and here's what i came up with. Am i right. Also if someone could be kind enough to explain me how can i be sure that my threads have finished.

Code:
// --- MyThread.java ---

public class MyThread Thread 
{
    //... 
    
    private volatile boolean live;

    public void run() 
    {
        while(live) 
        {
           //Do something 
           //Lets say sleep for 10 seconds 
           sleep(1000);	   
        }
    }

    //...
}



// ---- MyMultiThreadedApplication.java ---

public Class MyMultiThreadedApplication
{
    MyThread thread1;
    MyThread thread2;
    MyThread thread3;
    ...   

    public MyMultiThreadedApplication()
    {
       thread1=new MyThread("thread1");      
       thread2=new MyThread("thread2");
       thread3=new MyThread("thread3"); 
    }
     
    public StartAll()
    {
       thread1.start();
       thread2.start();
       thread3.start();    
    }
    
    public StopAll()
    {
       thread1.live=false;
       thread2.live=false;
       thread3.live=false;

       //How can i be sure here that the threads have finished ?        
    }    	
}

Your help is much appreciated
 
a)
thread1.live=false;

live has private access in MyThread, so you need to create a public method, to set it false.

b) How are you sure single-threaded programs have finished?
Why do you need it?

c) Is 3 the maximum? What platform?

seeking a job as java-programmer in Berlin:
 
Thank you again for your reply Stefan,

a) you are absolutely right thanks for pointing that out

b)
I need to know if the thread have stopped or not because the threads i will be creating maybe doing something which can never be left in the middle. In other words once i decide to stop the threads i can not continue doing anything else until the threads are trully finished. Setting live property to false will simply guarantie me that next time the thread gets a chance to check its while(live) condition the thread will jump out of the loop. In other words the threads may still be executing after StopAll is finished.

While reading i found out a little about Thread.join method. The documentation described it as the one to use to wait for a thread to die. But to be honest with you i fear that this Thread.join method maybe just as problematic as they describe Thread.stop and Thread.suspend to be. Thats why i need the opinion of you guys who have experience with these methods.

Code:
public StopAll()
    {
       thread1.live=false;
       thread2.live=false;
       thread2.live=false;
   
       //Is this a good  way to wait for the threads to die 
       thread1.join();
       thread2.join();
       thread3.join();    
    }

c)

My application will run on Windows XP platform and i may have many threads running i need to know the maximum preferably by looking at some sort of system setting so that i may make that decision at runtime.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top