The Artima Developer Community
Sponsored Link

Java Answers Forum
Holding on to Threads/Runnables (ArrayList) to be able to .STOP them

1 reply on 1 page. Most recent reply: Apr 13, 2005 4:55 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Shaitan

Posts: 11
Nickname: shaitan00
Registered: Feb, 2005

Holding on to Threads/Runnables (ArrayList) to be able to .STOP them Posted: Apr 13, 2005 3:11 AM
Reply to this message Reply
Advertisement
Coding Style: NetBeans IDE 4.0 Beta2 (Java)

My code (a thread, actually "implements runnable") spawns threads (runnables) to handle listening for incomming messages and stores them in an ArrayList (I could not think of a better way, please propose a better method if possible).
This is done as such:


public static ArrayList chList;

public void run() {
chList = new ArrayList();
chList.add(new serverListen());


This should allow me, when I want to close my main thread (runnable), to close all my listener threads (serverListen runnable) using the ArrayList (chList).
-- Quick question, I can do a .STOP on my listener threads (serverListen runnable) from this main thread right? (I create a public void stop(){ thread.stop;} function in my serverListen threads, I can do this right? if not how am I supposed to kill a thread from this scope? ---

So my problem is unlike a normal Array the ArrayList doesn't seem to allow me to go through each index and do a serverListen.stop()

So my goal is to do something like
for (int i = 0; i < chList.size; i++)
{
chList.stop()
}
Of course this is not working for many reasons (doesn't like "chList", and "chList.stop" doesn't seem to be recognized either).
Is there anyway to do this? A better way?
Any help would be GREATLY appreciated, thanks.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Holding on to Threads/Runnables (ArrayList) to be able to .STOP them Posted: Apr 13, 2005 4:55 AM
Reply to this message Reply
I don't get it, I'm afraid.
Everytime you run the Thread a new List should becreated? Why is that?

Personally, I don't have a clue what ArrayList is good for (except for the toArray method).

In this case I use a Vector, because I don't know how to use the ArrayList.

class ThisThread implements Runnable {
    public static Vector chList = null;
    public void run() {
    	if (chList == null) //this should solve your main problem
            chList = new ArrayList();
        ServerListen srvL = new ServerListen();
        srvL.start();
        chList.add(new ServerListen(srvL));
    }
    
    public void stop() {
    	while (!chList.isEmpty()) {
            ((ServerListen)(chList.get(0))).stop();
            chList.remove(0);
    	}
    }
}


1. Why is this a Runnable? do you think it takes to much time to create and start a thread?

2. Allways use capital letters for classnames. So I changed serverListen to ServerListen

Flat View: This topic has 1 reply on 1 page
Topic: How to get GUI panels out of another class? Previous Topic   Next Topic Topic: My JSlider does not work

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use