Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Big Question in Synchronized

 
Ernest Lee
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?
 
Dirk Schreckmann
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.
 
Jim Yingst
Wanderer
Sheriff
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.
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!