hello , my code is like :
Quote:
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

