Results 1 to 4 of 4
  1. #1
    chikaman is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    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 ExchangeContract, 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();
    }


    }

  2. #2
    renamed is offline Member
    Join Date
    Dec 2009
    Location
    Rio de Janeiro
    Posts
    38
    Rep Power
    0

    Default

    You need to keep a reference to the thread. Try "interrupt" method, then.
    Please don't laugh at my English... I'm trying my best! :)

  3. #3
    chikaman is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    could u please give me a code example

  4. #4
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    Java Code:
    Thread.currentThread().interrupt();
    If it does not work, please provide an executable snippet of your code. Otherwise it is difficult to follow your problem.

Similar Threads

  1. kill a thread
    By Peter in forum Advanced Java
    Replies: 6
    Last Post: 06-22-2010, 08:08 AM
  2. How to stop Thread
    By ersachinjain in forum Threads and Synchronization
    Replies: 2
    Last Post: 11-30-2009, 07:11 PM
  3. Thread won't stop
    By bubbless in forum Threads and Synchronization
    Replies: 15
    Last Post: 10-20-2009, 10:58 PM
  4. How to stop/pause running third party exe?
    By 2bGeek in forum AWT / Swing
    Replies: 3
    Last Post: 02-28-2009, 07:26 AM
  5. how to stop a thread
    By willemjav in forum Advanced Java
    Replies: 19
    Last Post: 09-10-2008, 07:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •