How to sychronize threads in different classes?

J

Jan Li?e

I have got a main class and this main class has a member-variable
with a boolean value. When a certain method of this class is called, the
method checks if my boolean-variable is true or not and does something (or not).
In the background is running the thread of my NetworkReceiver class
which listens on an existing socket for data. If some sort of data
arrives i want the thread to change the boolean variable of my main class.
My problem is: how can i synchronize the background thread with my
main class?
Many thanks in advance,
Jan
 
C

Chris Smith

Jan said:
I have got a main class and this main class has a member-variable
with a boolean value. When a certain method of this class is called, the
method checks if my boolean-variable is true or not and does something (or not).
In the background is running the thread of my NetworkReceiver class
which listens on an existing socket for data. If some sort of data
arrives i want the thread to change the boolean variable of my main class.
My problem is: how can i synchronize the background thread with my
main class?

Generally speaking, you can put synchronization locks near the data.
The easiest way to do this is to lock the object containing the boolean
field before you read or write the value. Alternatively, if you're
already using the object lock for a different purpose, you could create
a new object sitting beside the boolean field, and use that for a lock.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top