Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How do I stop or kill a running thread when a condition is true

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I stop or kill a running thread when a condition is true

    I have a class ImpServer which has a method accept, which a client can call by Remote Method Invocation. Accept method has an inner class which implements runnable. I try to use return to exit from the run method if nm is equal to userName. But there is no exit from the run method. What I actually want to do is to stop the thread from running. I don't no how to do this. Please somebody help me

    public class ImpServer extends UnicastRemoteObject implements Contract, Runnable{
     
    public void accept(final String nm) throws RemoteException {
    	new Thread(new Runnable() {
    		public void run() {
    		synchronized(client){
    			for(int i=0; i<client.size(); i++){
    				String userName = (String)client.get(i);
    				if(nm.equals(userName)){
    					try{
    					cc.notifyDuplicateName("Duplicate names”)
    					}catch(Exception e){}
    					return;
     
    				}
    			}
    		}
     
    		try {
     
    			synchronized(client) {
    				client.add(nm);
    				send(nm);
    				ss = new Sub(nm);
    				sData.add(ss);
     
    			}
    		} catch(Exception e) {
                      System.err.println(e);
            }
     
    	}
    	}).start();
    	}
    }
    Last edited by helloworld922; December 18th, 2009 at 12:08 AM. Reason: Please use [code] tags!


  2. #2
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: How do I stop or kill a running thread when a condition is true

    You can use thread.interrupt()

    Java 2 Platform SE 5.0

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: How do I stop or kill a running thread when a condition is true

    The interrupt method will only set the interrupted boolean to true but it wont actually kill the thread. The thread dies once it reaches the end or another exception is throw. You can always throw a runtime exception in the thread.

    Where exactly does the thread lock up and stay in a running state?

    // Json

  4. #4
    Junior Member
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I stop or kill a running thread when a condition is true

    Just add a bit in the run method to deal with the interrupted flag...

  5. #5
    Junior Member
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I stop or kill a running thread when a condition is true

    thanks for all your post

Similar Threads

  1. Problem with condition
    By shamed in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 7th, 2009, 04:51 PM
  2. Replies: 2
    Last Post: November 26th, 2009, 08:45 AM
  3. running PocketSphinx on SonyEricsson K750i
    By johnyjj2 in forum The Cafe
    Replies: 0
    Last Post: November 5th, 2009, 07:34 AM
  4. How to kill proceses
    By oyekunmi in forum Java Theory & Questions
    Replies: 3
    Last Post: October 16th, 2009, 09:34 AM
  5. Replies: 1
    Last Post: March 3rd, 2009, 08:04 AM