wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Multiple threads..? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Multiple threads..?" Watch "Multiple threads..?" New topic
Author

Multiple threads..?

malcolm bailey
Greenhorn

Joined: Aug 10, 2001
Posts: 22
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
Alex Zhang
Ranch Hand

Joined: Apr 19, 2001
Posts: 68
If U mean that class will have two instances of thread, surely U can.
malcolm bailey
Greenhorn

Joined: Aug 10, 2001
Posts: 22
..I'm not sure how to do it, though, as I'm fairly new to this..
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Aug 10, 2001
Posts: 22
Thanks Peter, I'll see what I can do...
Leela
Greenhorn

Joined: Jan 25, 2002
Posts: 8
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


Leela
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Multiple threads..?
 
Similar Threads
Using threads effectively
SwingWorker Threads
Initial Context Lookup - Basic Question
another small puzzle
GUI update error