• 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
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Multi-Threaded program using a singleton object  RSS feed

 
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
 
author
Sheriff
Posts: 23608
138
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!