Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Fubarable

Thread: how to kill a thread

  1. #1
    know_how is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default how to kill a thread

    hello , my code is like :
    import java.util.*;
    class MyThread implements Runnable {
    Scanner s = new Scanner (System.in);
    String p ;
    String threadname ;
    Thread t ;

    MyThread(String s1){
    threadname = s1 ;
    t = new Thread (this , threadname);
    t.start();
    }
    public void run() {
    System.out.println("Thread in to : " +Thread.currentThread().getName());
    System.out.print("Enter name with in 5 seconds : ");
    p = s.next();
    }
    }
    public class threadassn {
    public static void main(String args[])throws Exception{
    MyThread m1 = new MyThread("My Thread 1");
    try{
    System.out.println("Running Thread Name :" +Thread.currentThread().getName());
    Thread.sleep(5000);
    }catch(Exception e){}

    if((m1.p)==(null))
    {
    System.out.println("\nOops!!! time out!!!");
    System.out.println(m1.t.isAlive());
    // System.exit();
    }
    else
    System.out.println("\n"+m1.p);
    }
    }

    actually i have created a thread m1 in main and when main thread sleep() for 5 sec , i want to enter my name and if the user doesnot enter something within 5 sec i want this thread to stop , and my program should terminate .

    everything goes well when i enter something in 5 sec it show the entered text after 5 sec but when i donot enter anything and wait for timeout , the execution waits for me to enter something after printing timeout i.e. the program actually holds at s.next() , i know that i can terminate the program by using System.exit() , but i want a different approach so that thread is killed at that very instant after 5 sec...

    actually my concern is when i want to terminate OR kill a single thread like this out of some 10 threads in process


    please help me with a solution and appropriate explaination

    thanks in advance

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: how to kill a thread

    Please use [code] [/code] tags when posting code, not [quote] [/quote] tags so that we can read and understand your code.

    I'm no expert at "killing" threads, but have you tried putting code in the empty catch block and calling interrupt on the thread that you wish to kill? If so, what happens?

  3. #3
    know_how is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: how to kill a thread

    Java Code:
     import java.util.*;
    
     class MyThread implements Runnable 
     {
        Scanner s = new Scanner (System.in);
        String p ; 
        String threadname ;
        Thread t ;
    
        MyThread(String s1)
        {
        threadname = s1 ;
        t = new Thread (this , threadname);
        t.start();
        }
    
        public void run() 
        {
        System.out.println("Thread in to : " +Thread.currentThread().getName());
        System.out.print("Enter name with in 5 seconds : ");
        p = s.next();
        }
     }
    
     public class threadassn 
     {
        public static void main(String args[])throws Exception
        {
        MyThread m1 = new MyThread("My Thread 1");
        try{
              System.out.println("Running Thread Name :" +Thread.currentThread().getName());
              Thread.sleep(5000);
            }catch(Exception e){}
    
        if((m1.p)==(null))
        {
          System.out.println("\nOops!!! time out!!!");
          System.out.println(m1.t.isAlive());
           // System.exit();
         }
         else
         System.out.println("\n"+m1.p);
         }
      }
    i didn't got it exactly please elaborate OR give the approriate code

    actually i have created a thread m1 in main and when main thread sleep() for 5 sec , i want to enter my name and if the user doesnot enter something within 5 sec i want this thread to stop , and my program should terminate .

    everything goes well when i enter something in 5 sec it show the entered text after 5 sec but when i donot enter anything and wait for timeout , the execution waits for me to enter something after printing timeout i.e. the program actually holds at s.next() , i know that i can terminate the program by using System.exit() , but i want a different approach so that thread is killed at that very instant after 5 sec...

    actually my concern is when i want to terminate OR kill a single thread like this out of some 10 threads in process


    please help me with a solution and appropriate explaination

    thanks in advance
    Last edited by know_how; 01-29-2013 at 07:11 PM. Reason: not prop

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: how to kill a thread

    Your code is all left-justified making it hard to read. Is this how you normally format your code? Please edit and add indentations as this will make it possible for us to read it.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: how to kill a thread

    Your thread will die eventually, as the run() method has exited.
    There's not much more you can do.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    know_how is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: how to kill a thread

    Java Code:
    import java.util.*;
     
    class MyThread implements Runnable
    {
       Scanner s = new Scanner (System.in);
       String p ;
       String threadname ;
       Thread t ;
     
       MyThread(String s1)
       {
       threadname = s1 ;
       t = new Thread (this , threadname);
       t.start();
       }
     
       public void run()
       {
       System.out.println("Thread in to : " +Thread.currentThread().getName());
       System.out.print("Enter name with in 5 seconds : ");
       p = s.next();
       }
    }
     
    public class threadassn
    {
       public static void main(String args[])throws Exception
       {
       MyThread m1 = new MyThread("My Thread 1");
       try{
             System.out.println("Running Thread Name :" +Thread.currentThread().getName());
             Thread.sleep(5000);
           }catch(Exception e){}
     
       if((m1.p)==(null))
       {
         System.out.println("\nOops!!! time out!!!");
         System.out.println(m1.t.isAlive());
          // System.exit();
        }
        else
        System.out.println("\n"+m1.p);
        }
     }
    i didn't got it exactly please elaborate OR give the approriate code

    actually i have created a thread m1 in main and when main thread sleep() for 5 sec , i want to enter my name and if the user doesnot enter something within 5 sec i want this thread to stop , and my program should terminate .

    everything goes well when i enter something in 5 sec it show the entered text after 5 sec but when i donot enter anything and wait for timeout , the execution waits for me to enter something after printing timeout i.e. the program actually holds at s.next() , i know that i can terminate the program by using System.exit() , but i want a different approach so that thread is killed at that very instant after 5 sec...

    actually my concern is when i want to terminate OR kill a single thread like this out of some 10 threads in process


    please help me with a solution and appropriate explaination

    thanks in advance

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: how to kill a thread

    Quote Originally Posted by know_how View Post
    actually i have created a thread m1 in main and when main thread sleep() for 5 sec , i want to enter my name and if the user doesnot enter something within 5 sec i want this thread to stop , and my program should terminate .
    No, you have created a Runnable, m1, in your main method. The MyThread class is misleadingly named since it is not in fact a Thread, and the Thread object itself is held inside this class in variable "t".

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: how to kill a thread

    DOH!
    That was stupid of me...of course it hasn't exited.
    It's sat waiting for Scanner input.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: how to kill a thread

    Quote Originally Posted by Tolls View Post
    Your thread will die eventually, as the run() method has exited.
    There's not much more you can do.
    His run() method has a blocking call on a Scanner object, and so will never die until input has been given by the user.

  11. #11
    know_how is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

Similar Threads

  1. kill a thread
    By Peter in forum Advanced Java
    Replies: 6
    Last Post: 06-22-2010, 09:08 AM
  2. How do I stop or kill a running thread when a condition is true
    By chikaman in forum Threads and Synchronization
    Replies: 3
    Last Post: 12-22-2009, 12:07 AM
  3. Kill sessions
    By Subscribe.name in forum Java Servlet
    Replies: 1
    Last Post: 10-05-2009, 11:22 PM

Posting Permissions

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