• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multi-Threaded program using a singleton object

 
d jones
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is a question about a multi-threaded program.

Imagine we have a singleton class and we call an instance of it from
2 different threads.

Both threads will now have a reference to the same object in memory.

If the singleton class has a mehod which isn't synchronized and it accepts
2 numbers and just adds them.

If we call this method from Thread 1 and pass in 2 numbers
and also call it from thread 2 and pass in 2 different numbers.
This method doesn't use any class variables and only works on the parameters passed in.

Will the 2 calls to this method always operate independently of each other?
Or would there ever be a chance where the 2 numbers passed in from thread 1 and thread 2 could get confused and add the first parameter from thread 1 to the second parameter from thread 2?

Many Thanks,
Dan
 
Henry Wong
author
Sheriff
Posts: 22526
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by d jones:
Hi,
If the singleton class has a mehod which isn't synchronized and it accepts
2 numbers and just adds them.

If we call this method from Thread 1 and pass in 2 numbers
and also call it from thread 2 and pass in 2 different numbers.
This method doesn't use any class variables and only works on the parameters passed in.

Will the 2 calls to this method always operate independently of each other?
Or would there ever be a chance where the 2 numbers passed in from thread 1 and thread 2 could get confused and add the first parameter from thread 1 to the second parameter from thread 2?


In the condition that you described, the two threads will operate independently of each other.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!