• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Thread not existing

 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote a simple threadpool that's been working very well for me. However, my program would never exit since I have child threads and run. I usually run 10 threads at a time, with about 2000 tasks to perform. After all the tasks have been executed, the active threads would never exit. Anyway I can work around it?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use "setDaemon(true)" to turn all the pool threads into daemon threads.
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My understanding is daemon thread would just exit after execution completed. But the scenario for me is that I don't know how many worker objects I would add. So I initialize the pool with 10, but I could be adding 100 to it. So daemon thread would just execute the first 10 and then exit, leaving other 90 not running.
Is my view correct?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By definition, the JVM will exit when the only running threads are "daemon" threads. Having a single non-daemon thread still running will keep the application alive. So whichever thread either has the list of work, or receives the requests for work, should be a non-daemon thread, and it gets to decide when the application should exit by terminating itself.
Am I missing something?
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my basic design:
I have a thread pool, it basically maintains the threadpool. I have a worker thread, and does the execute, calling run method etc.
The thread pool uses a simple ArrayList to keep a list of available threads.
When pool.addWork(Runnable) is called, and hands it off to WorkerThread, which add itself to ArrayList workingList and does the work.
My worker thread essentially runs if there is work to be done, and waits until there is more work. This is the part I get stuck on, like my previous post states, I don't know how many tasks I need to add, or the time lapse between the adding. I understand I need to figure out a wait to break out of the wait() state with interrupt which I can do thru thread pool class. I just don't know when I should invoke it. Also, I would like to make it self-terminating, is that possible?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way that you could keep track of how many incomplete tasks there are in the system? Like with a counter that you increment every time a taks is assigned, and decremement every time a task is completed? When the count reaches zero, you could just call System.exit(0). Of course, you'd have to make sure that check isn't made until after you've started some tasks, or the system will exit before it begins.
There are of course other ways to track incomplete tasks. You might have a List of tasks which have not been completed, for example. Or perhaps two - one List of tasks which had not even been started yet, and another List of tasks which were being worked on by a thread. Once both lists have size() == 0, exit.
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, that's exactly what I had in mind. But how do I check is a thread is in wait state. I mean the isAlive() method doens't excatly provide that functionality.
 
He baked a muffin that stole my car! And this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic