Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Communication - Starting and Stopping the threads  RSS feed

 
Chinni Bujji
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In my web application i have started some threads
There is a start button which starts the thread there is a stop button which stops the thread
But if the user goes away without clicking the stop button the threads are hanging there with a continuous refresh interval

So i need some mechanism which i can stop those threads when the user goes away from that page
or if the user closes the browser then i need to stop all the running threads

please help me with some code or some ideas
any ideas are appreciated

thanks
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you need a mechanism to tell the threads to stop? Or is your question how to know when you should tell the threads to stop?

(By the way if you're talking about a web application, there is no practical way to know when a user stops communicating with your application.)

 
raghu mrvreddy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...







 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!