Results 1 to 11 of 11
Thread: how to kill a thread
- 01-29-2013, 05:27 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 6
- Rep Power
- 0
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
-
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?
- 01-29-2013, 05:49 PM #3
Member
- Join Date
- Jan 2013
- Posts
- 6
- Rep Power
- 0
Re: how to kill a thread
i didn't got it exactly please elaborate OR give the approriate codeJava 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); } }
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 advanceLast edited by know_how; 01-29-2013 at 06:11 PM. Reason: not prop
-
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.
-
Re: how to kill a thread
Here's a non-Scanner way of interrupting user input on the console: [JavaSpecialists 153] - Timeout on Console Input
- 01-29-2013, 06:16 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
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.
- 01-29-2013, 06:17 PM #7
Member
- Join Date
- Jan 2013
- Posts
- 6
- Rep Power
- 0
Re: how to kill a thread
i didn't got it exactly please elaborate OR give the approriate codeJava 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); } }
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
-
Re: how to kill a thread
- 01-29-2013, 06:18 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
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.
-
Re: how to kill a thread
- 01-29-2013, 06:33 PM #11
Member
- Join Date
- Jan 2013
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
kill a thread
By Peter in forum Advanced JavaReplies: 6Last Post: 06-22-2010, 08:08 AM -
How do I stop or kill a running thread when a condition is true
By chikaman in forum Threads and SynchronizationReplies: 3Last Post: 12-21-2009, 11:07 PM -
Kill sessions
By Subscribe.name in forum Java ServletReplies: 1Last Post: 10-05-2009, 10:22 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks