I am not sure how you are keep tracking of the threads for each user but its always better to maintain a thread pool and assign a thread for each request of user when he starts a thread..
Here is a sample program which starts a thread running indefinitely and i stop the thread from main thread --- code might use some deprecated methods but it works
Copy and paste it and it should work... I used some existing code from internet.... @http://nadeausoftware.com/articles/2008/04/java_tip_how_list_and_find_threads_and_thread_groups
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
public class TestClass {
public static void main(String[] s){
TestClass t = new TestClass();
t.xxx();
}
public void xxx(){
Thread t1 = new Thread(new Runnable() {
public void run() {
for(;;){
try {
System.out.println("got in");
//Thread.sleep(1000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
},"one");
t1.start();
System.out.println("Getting thread Info");
Thread t = getThread("one");
System.out.println(t);
//changing the flag
//comment and uncomment the t.stop() which is another thread i started
t.stop();
}
Thread getThread( final String name ) {
if ( name == null )
throw new NullPointerException( "Null name" );
final Thread[] threads = getAllThreads( );
for ( Thread thread : threads )
if ( thread.getName( ).equals( name ) )
return thread;
return null;
}
Thread[] getAllThreads( final Thread.State state ) {
final Thread[] allThreads = getAllThreads( );
final Thread[] found = new Thread[allThreads.length];
int nFound = 0;
for ( Thread thread : allThreads )
if ( thread.getState( ) == state )
found[nFound++] = thread;
return java.util.Arrays.copyOf( found, nFound );
}
Thread[] getAllThreads( ) {
final ThreadGroup root = getRootThreadGroup( );
final ThreadMXBean thbean = ManagementFactory.getThreadMXBean( );
int nAlloc = thbean.getThreadCount( );
int n = 0;
Thread[] threads;
do {
nAlloc *= 2;
threads = new Thread[ nAlloc ];
n = root.enumerate( threads, true );
} while ( n == nAlloc );
return java.util.Arrays.copyOf( threads, n );
}
ThreadGroup getThreadGroup( final String name ) {
if ( name == null )
throw new NullPointerException( "Null name" );
final ThreadGroup[] groups = getAllThreadGroups( );
for ( ThreadGroup group : groups )
if ( group.getName( ).equals( name ) )
return group;
return null;
}
ThreadGroup[] getAllThreadGroups( ) {
final ThreadGroup root = getRootThreadGroup( );
int nAlloc = root.activeGroupCount( );
int n = 0;
ThreadGroup[] groups;
do {
nAlloc *= 2;
groups = new ThreadGroup[ nAlloc ];
n = root.enumerate( groups, true );
} while ( n == nAlloc );
ThreadGroup[] allGroups = new ThreadGroup[n+1];
allGroups[0] = root;
System.arraycopy( groups, 0, allGroups, 1, n );
return allGroups;
}
ThreadGroup rootThreadGroup = null;
ThreadGroup getRootThreadGroup( ) {
if ( rootThreadGroup != null )
return rootThreadGroup;
ThreadGroup tg = Thread.currentThread( ).getThreadGroup( );
ThreadGroup ptg;
while ( (ptg = tg.getParent( )) != null )
tg = ptg;
return tg;
}
}
Hope this helps... I might me completely wrong too...