Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Block thread until callback method has been called by other thread

 
James Collett
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply. I have managed to RESOLVE this problem using the code below.

 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic