Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

communucation of two threads

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how do u communicate two threads?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What kind of communication do you see a need for? Some simple things are pretty normal in spite of threads being involved.
Say ObjectA is running in Thread1 and ObjectB is runing in Thread2. And say B is doing some kind of loop which is why we put it in a thread in the first place. And we'd like to tell it to stop. ObjectA can call a method on ObjectB that simply sets a variable, say keepRunning=false. That method call runs on Thread1. But the next time B goes through its loop on Thread2, it can look at the variable and see that it has been told to stop.
Another kind of thread communication happens in Swing. If you're doing a long-running task on its own thread so the Swing thread stays responsive to mouse clicks and such, and you want the task to update the GUI you should not directly update Swing widgets from the task thread. It confuses Swing and causes repaint problems. So there is a SwingUtil class that lets you put a command into a queue from the task thread. Some time soon the Swing thread sees the command on the queue - much like the variable above - and executes the command in the Swing thread. Pretty clever.
Do you have something trickier in mind? There's a whole branch of computer science in threading, semaphores, joining, etc.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is another way to communicate. You can always send an interrupt to a sleeping or waiting thread and catch the exception and do whatever you want to in the catch block. Hope this helps.
Vinod.
'

Originally posted by eswar kumar:
how do u communicate two threads?

 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic