-1

I have two Threads, ThreadA (Main) with the methods j() and k(){...j()...} and a second Thread, ThreadB. ThreadA randomly calls method k(), which, among others, calls method j(). ThreadB randomly calls method j().

Now method k() and j() can not be executed at the same time as they share variables.

How can I make sure that ThreadB is only able to call method j() if ThreadA is not currently executing method k()? And also that ThreadB will execute j() after ThreadA finished executing k().

I read about the synchronized keyword but it seems that it can only cover one method. (e.g. ThreadX and ThreadY both access method m()).

Erveron
  • 1,908
  • 2
  • 25
  • 48

1 Answers1

0

I would use the mechanism of synchronized statements and intrinsic locks. You can read about it here. You can synchronize both methods using synchronized statements pointing to the same object.

Karzmu
  • 142
  • 1
  • 9