• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Big Question in Synchronized

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have 2 methods in a class, let's say method A and method B.
If i want B to be stopped whenever A is doing something.. how can i implement it?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only one method of a class is ever operating at a given time - at least I can't think of an example where this isn't the case (excepting threads and multiple objects of a class).
If you are talking about methods from two different objects of the same class, then you may wish to implement threads to get the job done. Take a look at Sun's Java Tutorial on Threads - especially the section on Synchronization.
Good Luck.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only one method of a class is ever operating at a given time - at least I can't think of an example where this isn't the case (excepting threads and multiple objects of a class).
Errr... I assumed this was talking about multiple threads. Why else consider the synchronized keyword? It's certainly possible for two or more threads to have access to the same instance at the same time, and thereby run two different methods of that instance at the same time - or even the same method, from two different threads.
Or maybe you're referring to the fact that the processor is only acting on one thread at a time? This is true (unless you have multiple processors, which is certainly possible) but since either thread could interrupt the other at any time, the threads are generally thought of as running at the same time.
To be honest, this question doesn't seem like "Advanced Java" as much as "Basic Threads and Synchronization". Dirk's right - read up on the Sun tutorial. For further questions, we also have a Threads and Synchronization forum here at JavaRanch, which is the best place for this sort of thing. I'm moving this discussion there now.
 
I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic