permaculture playing cards*
The moose likes Threads and Synchronization and the fly likes Block thread until callback method has been called by other thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Block thread until callback method has been called by other thread" Watch "Block thread until callback method has been called by other thread" New topic
Author

Block thread until callback method has been called by other thread

James Collett
Greenhorn

Joined: May 11, 2012
Posts: 2
Hi All,

Please can you advise best way to block or suspend an application thread, until another thread has called a method in the application's class.

Thank you for your time and help,
Best regards,
James

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

If you're asking what I think you're asking, then I usually use a java.util.concurrent.CountdownLatch for that. Something like this, though you'll have to double check method names and args in the docs:



And welcome to the Ranch!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

James, please BeForthrightWhenCrossPostingToOtherSites(⇐click) so that people don't waste their time duplicating others' answers. Thanks!


https://forums.oracle.com/forums/thread.jspa?threadID=2388004&tstart=0
James Collett
Greenhorn

Joined: May 11, 2012
Posts: 2
Thank you for your reply. I have managed to RESOLVE this problem using the code below.

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4571
    
    5

Jeff Verdegan wrote:James, please BeForthrightWhenCrossPostingToOtherSites(⇐click) so that people don't waste their time duplicating others' answers. Thanks!


https://forums.oracle.com/forums/thread.jspa?threadID=2388004&tstart=0

Also at
http://www.java-forums.org/threads-synchronization/59606-block-thread-until-callback-method-has-been-called-other-thread.html
http://www.javaprogrammingforums.com/threads/15650-block-thread-until-callback-method-has-been-called-other-thread.html

edit
http://forums.devshed.com/java-help-9/block-thread-until-callback-method-has-been-called-by-other-904920.html
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
I found such codes useful on 2 occasions:

1) memory: when main thread's default java memory is not enough; main thread has fixed memory while child threads can be set

2) on java gui on desktop or applets: when main thread is doing some processing and another request comes to update gui, the request is not reflected; a child thread can be used to execute the gui request.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jesus Angeles wrote:I found such codes useful on 2 occasions:

1) memory: when main thread's default java memory is not enough; main thread has fixed memory while child threads can be set


No, it doesn't work that way. The JVM's total memory is set at startup, and it is shared by all threads. Using multiple threads doesn't help with memory usage.
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
Jeff Verdegan wrote:
Jesus Angeles wrote:I found such codes useful on 2 occasions:

1) memory: when main thread's default java memory is not enough; main thread has fixed memory while child threads can be set


No, it doesn't work that way. The JVM's total memory is set at startup, and it is shared by all threads. Using multiple threads doesn't help with memory usage.


I should have specified that I was talking about ThreadStackSize.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jesus Angeles wrote:
Jeff Verdegan wrote:
Jesus Angeles wrote:I found such codes useful on 2 occasions:

1) memory: when main thread's default java memory is not enough; main thread has fixed memory while child threads can be set


No, it doesn't work that way. The JVM's total memory is set at startup, and it is shared by all threads. Using multiple threads doesn't help with memory usage.


I should have specified that I was talking about ThreadStackSize.


The same thing applies though. You can specify the stack size at startup. I don't know off the top of my head if that's a total size or a per-thread stack size, but it doesn't matter--if you're running out of stack, you can make it bigger. And running out of stack should be a rare occurrence anyway. If you're using multiple threads to alleviate a memory problem, you're doing it wrong.
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
Jeff Verdegan wrote:
Jesus Angeles wrote:
Jeff Verdegan wrote:
Jesus Angeles wrote:I found such codes useful on 2 occasions:

1) memory: when main thread's default java memory is not enough; main thread has fixed memory while child threads can be set


No, it doesn't work that way. The JVM's total memory is set at startup, and it is shared by all threads. Using multiple threads doesn't help with memory usage.


I should have specified that I was talking about ThreadStackSize.


The same thing applies though. You can specify the stack size at startup. I don't know off the top of my head if that's a total size or a per-thread stack size, but it doesn't matter--if you're running out of stack, you can make it bigger. And running out of stack should be a rare occurrence anyway. If you're using multiple threads to alleviate a memory problem, you're doing it wrong.


It is not unusual in my industry (which I prefer not to mention) to require a high stack size.

You are right. The stack size can be set at jvm startup. However, the 'main' thread's stack size is set by each OS. I should have specified, that in the OS I use (Windows XP), the main thread doesnt get the jvm startup setting, because of the reason just mentioned. All child threads however does get the setting.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jesus Angeles wrote:However, the 'main' thread's stack size is set by each OS. I should have specified, that in the OS I use (Windows XP), the main thread doesnt get the jvm startup setting, because of the reason just mentioned.


Ah, interesting. I wasn't aware of that. (Although I'll be skeptical if it's really the case until I observe it myself or see documentation supporting it.)
 
GeeCON Prague 2014
 
subject: Block thread until callback method has been called by other thread