Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Big Question in Synchronized

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
There's a city wid manhunt for this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic