1

Here is a sample code if the thread size is 3 means its ok. where i can manage the memory. if the thread size is 50 there the problem lies. i need to set threadsize as 5. finished thread should be reuse the by other

Thread[] TCreate = new Thread[iThreadSize]; 
for (int i = 0; i< TCreate.length; i++) {

    TCreate[i] = new Thread(new Runnable() {
        public void run() {
            lst.Add(this.getResult(url));
        }
    });
    TCreate[i].setName("URL"+i);
    TCreate[i].start(); }

for (int j = 0; j < TCreate.length; j++)
    while (TCreate[j].isAlive())
         Thread.sleep(10);

Can any one help what is use of setDaemon() method. what is purpose of Daemon Please Help me.. Advance thanks

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
Syed Abdul Kather
  • 608
  • 2
  • 12
  • 29

2 Answers2

2

setDaemon controls whether a thread is a daemon thread or not. If daemon threads are still running when the program reaches the end of the main method, it does not keep the program from quitting. Non-daemon threads (user threads) do keep the program from quitting.

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
1

Java already includes methods for managing Thread pools.

calling Executors.newFixedThreadPool(5) will generate a thread pool with 5 worker threads for you.

Afterwards you can just assign Runnables that will be executed by on of the Threads in the pool.

See also:

Community
  • 1
  • 1
Robert
  • 39,162
  • 17
  • 99
  • 152