• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Two threads of same class instance to wait separately

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class that extends from Thread and I have two or more them running, they have their own queus to work on with.

When the queue is in some point empty then sometimes I want them to pause because thread that fills their queue is in Thread.WAITING state.

When the first thread whose queue is empty calls method that causes it to wait e.g.



Now the first thread calls this succesfully, but second and other same class instances of this thread never gets this far, so are they in WAITING state too when the first does the pause call. They all have different names.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If they are running in different Threads, then this code will NOT put those other Threads in a waiting state. What is going on is hard to tell without more code.

"Class that extends from Thread and I have two or more them running"
Two or more different instances or two or more Threads running the same instance of the class?

 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for long reply...

Thanks, now it no longer extends from Thread but implements Runnable.

Code for the AbstractDataProcesser class.


AbstractDataLoader loads / fills queues, etc... Threads that runs AbstractDataProcesser's are created by ThreadFactory in AbstractDataLoader.

in goToSleep() method there is

which prints only once (i have two threads running), which is the problem

my first post was a bit messy, sorry for that
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And how are the two threads started?
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically pausing is not working..

And how are the two threads started?


in lines 300 and 75


 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have struggled with the synchronization for a long time now, to avoid consumption of memory (heap is setted to ~300MB or more) and be fast as it can be. With these classes I am comparing number permutations and there can be billion of them and it takes ages to process those
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I was looking for an answer to this question, posted earlier:
"(Are there) Two or more different instances (of the AbstractDataProcesser) or two or more Threads running the same instance of the class?"
The answer to that question seems to be in the createDataProcessers() method which you don't show.

So: Is there just one instance of the AbstractDataProcesser created, and inserted into the array of processers which gets run, or do you create one instance of AbstractDataProcesser, put it into the array twice, and then run both instances?

If there are multiple instances, then you should have to worry about synchronizing processing inside AbstractDataProcessor, and the error probably lies in the dataLoader, and the sharing of values between different threads inside that class (paused state, etc...). If there is one instance of the AbstractDataProcesser then we have to look at the data inside AbstractDataProcesser, since it is NOT thread safe, as well as the synchronization of methods. What is happening inside dataLoader is important too - but the narrowest focus first, we would need to make sure ADP is thread safe first, then expand out to the dataLoader.
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks,

So I should remove ADL from ADP's and program ADL so that it informs ADP's when everything should pause and when loading data to its queue is paused then ADP fires an event when it needs more, etc.
Firing events probably eats too much memory...

One way to use these classes

EDIT: code changed
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how threads are communicates in java.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abdulrub bin mohsin wrote:how threads are communicates in java.



Hi Abdulrub, this is a different question than what the OP was asking, so you should probably ask it in its own thread. It will attract more attention, won't detract from the previous conversation, and won't cause confusion.


And welcome to the JavaRanch!
reply
    Bookmark Topic Watch Topic
  • New Topic