Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Custom Thread Pool

 
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am creating a custom thread pool just to refresh the inner working of ThreadPool. Here are the classes:
MyTask


CustomThreads


CustomThreadPool


My problem is with custom threads. Here is the workflow. Initially thread pool will initialize a given number of thread and all threads will go into wait state. As and when thread pool is given more task, I want the waiting threads to awaken and start processing the tasks. I have tried to separate out all processes for a cleaner design.

Currently, all tasks are in wait state, but I am missing something important here, as the tasks are not being processed.

My assumption is the notifyAll() is at wrong place. Am I right?
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

s ravi chandran wrote:
My assumption is the notifyAll() is at wrong place. Am I right?



It is not "place". It is object/instance.... You must call notify on the same instance that is being used to wait on. Also, all the threads in the pool are waiting on different instances, so, if you want to notify them all, you will need to loop through all your thread instances.

Henry
 
s ravi chandran
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I added a loop and calling notify for each thread instance.

Here is my updated class:


It does work now, but is there a cleaner way to do it? looping threads and synchronizing each instance looks bad.

Also, how can I gracefully shutdown? updating isAlive in CustomThreadPool won't effect already waiting threads. Should I throw interrupt exception for these threads to exit?
 
Henry Wong
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

s ravi chandran wrote:
It does work now, but is there a cleaner way to do it? looping threads and synchronizing each instance looks bad.



Well, I would argue that it more than "looks bad". It actually is bad. Think about it. You submitted one task, and you have to wake all of the threads? Shouldn't you just wake up one thread?

With all of the threads synchronizing on separate objects, there is no way to only wake up one. Well, you can only send one notification, but, you don't know if that thread is actually waiting. So, to guaranteed to wake up one thread, you have to wake up all of the threads.  Also, the separate threads are on separate locks, so they will access the task list in parallel ... in an thread unsafe manner.

I highly recommend that you have all the thread synchronize the same instance, which additionally, is used to protect the data structures of the pool.

Henry
 
s ravi chandran
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Updated the code:

CustomQueue


CustomThread


CustomThreadPool


Does this look more optimal? Now the queue is handling the overflow and underflow cases.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic