File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Two threads of same class instance to wait separately Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Two threads of same class instance to wait separately" Watch "Two threads of same class instance to wait separately" New topic
Author

Two threads of same class instance to wait separately

Mikko Kohtamäki
Ranch Hand

Joined: Dec 13, 2008
Posts: 112
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.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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?


Steve
Mikko Kohtamäki
Ranch Hand

Joined: Dec 13, 2008
Posts: 112
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

And how are the two threads started?
Mikko Kohtamäki
Ranch Hand

Joined: Dec 13, 2008
Posts: 112
Basically pausing is not working..

And how are the two threads started?

in lines 300 and 75


Mikko Kohtamäki
Ranch Hand

Joined: Dec 13, 2008
Posts: 112
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Dec 13, 2008
Posts: 112
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
abdulrub bin mohsin
Greenhorn

Joined: Oct 29, 2009
Posts: 1
how threads are communicates in java.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two threads of same class instance to wait separately