简体   繁体   中英

How to implement Asynchronous Inter Thread Communication in java?

I am developing one code in which I have one parent thread and one child thread.Now, my scenario is like mentioned below,

  • Parent thread start the child thread
  • after starting the child thread it continuous to work what it is doing.
  • Now one time occurs that child thread want to call the method in the main thread
  • then it call asynchronously the main thread with that method.

I also want to clarify that I know the concurrency package in java but it is doing synchronously ie main thread have to wait for the child thread till it complete the execution,but I want the main thread to do continuous work while child thread executing.

My implementation is like SwingWorker Thread in java

You don't get to "call a method in the main thread". The only thing you can do is implement a specific mechanism whereby the main thread, by its own initiative, executes a method on an object which was provided by the child thread.

The above roughly describes what the Swing's mechanism does: the "main" thread (in that case, the Event Dispatch Thread) dequeues objects from a global queue and, if the object's type is appropriate, invokes the run method on an associated instance of Runnable .

Main point: in Swing the EDT doesn't "continue to work what it is doing"; it specifically waits for other threads to tell it what to do via this mechanism and otherwise just blocks, doing nothing at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM