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?
Joined: Dec 10, 2001
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.
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.