• Post Reply Bookmark Topic Watch Topic
  • New Topic

Intimate after thread completes

 
Ja King
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Three threads(Main thread, sub thread1 ,sub thread2) are simultaneously running , if any one of the thread completes it has intimate to the main thread.Please tell the solution asap.
 
Henry Wong
author
Sheriff
Posts: 22530
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can you elaborate what you mean by "intimate"? The definition that I normally use, don't seem to fit in this context.

Henry
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe he's using the original meaning of intimate, which is to announce. The noun form of this would be intimation, not intimate as above. Anyway, he seems to want the main thread to receive notification when either of the sub-threads completes.

Vijai, this can be achieved using a wait-notify protocol. The main thread would call wait() on a monitor, and the two sub-threads would call notify() when they complete, using the same monitor. This monitor can be any object, as long as all three threads have access to that object, and they must each synchronize on that object before calling wait() or notify(). You could also use a Lock and Condition from java.util.concurrent.locks.

Another possibility is to use an ExecutorService, also from java.util.concurrent. Here you probably wouldn't be creating and managing the two sub-threads directly - they'd be part of a pool managed by the ExecutorService, which you might create using Executors.newFixedThreadPool() or a similar method. The jobs of the two sub-threads would be defined using Runnable or Callable, and these would be submitted to the ExecutorService. Then you'd call invokeAny() on the ExecutorService, which will return as soon as one of the tasks has completed. I would probably use this technique, since the ExecutorService handles most of the details for you. The other techniques require more work from you - but they also give you more control over the details.
 
Ja King
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need sample program by using 1) wait , notify methods and 2 )ExecutorService.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good starting points:

1) wait and notify

2) ExecutorService

Words like "example" or "tutorial" can also help focus your results.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!