• 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
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

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
Posts: 23821
140
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
 
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!