Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Multiple threads..?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
I would like to know if it's possible to have two (or more) independent threads in the same class working concurrently, and if so, how do I do it? In my application, I'm trying to have a thread continually sending messages, and another continually listening.
Thanks in advance, Malc
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If U mean that class will have two instances of thread, surely U can.
 
malcolm bailey
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
..I'm not sure how to do it, though, as I'm fairly new to this..
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't expect any of the following to work, I'm just making it up as I go... what you're looking for is the classic producer/consumer thing. A message box.The above class implements a threadsafe message box that will hold a single message. There may be multiple threads posting messages, and/or multiple threads receiving messages. If you want to get fancy you can decouple senders and receivers by adding a buffer list that will hold more than one message, etc.
A simple sender could look likeThis sender will send a message through the mailbox every 0.5-1.5 seconds. A receiver could look likeWrite a main() that will create any number of senders and receivers, start() them, and off you go. After fixing the undoubted stupidities that crept in my code of course
- Peter
 
malcolm bailey
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter, I'll see what I can do...
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Alex Zhang:
If U mean that class will have two instances of thread, surely U can.


Hi,
I there are two/more instances of a thread &call the start method on all of them,how many run methods do we need and what will be the result.
Thanks
 
The knights of nee want a shrubbery. And a tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic