new to multithreading- how to use wait() and notify() in java? -


I am trying to write a program with 2 squares, one controller and one class that does a lot of computation. The controller makes some examples of the second class, then asks them to start counting them (in parallel). When they are finished, and the controller starts, and after some time, the controller passes them through new data, and they are re-calculated. Ideally, I will be able to call the initial () parameters, but this is not possible, so the controller calls a method in a calculator that stores the data in the global, and then calculates the thread and Starts the return, which works until it does not try to start the thread again, and it tells me the thread is dead so I am trying to run an eternal loop which That only waits till it is notified, then it is calculated, stores the results in the global, so that the controller can retrieve them later, then start waiting then something like this :

In Controller: Calculator calc = new calculator (); Calc.runCalculations (data); While (calc.isCalculating ()) {// nothing} System.out.println ("Result:" + calc.get Ansver ()); Calc.runCalculations (data2); // Calculator: Public Run Calculations (data data) {globalData = data; This.notify (); } Public run zero () {while (true) {synchronize (this) {wait (); } Calculation = true; GlobalData = Calculation (GlobalData); Calculation = false; }}

You will need to get the monitor at this Notify before calling () . In addition, when you call wait () , you should do this in a loop that checks a condition that you have not experienced fake spies.

  Public run calculations (data data) {globalData = data; Synchronize (this) {count = true; This.notify (); }} Run public null () {while (true) {synchronize (this) {count = false; While (count!) {Wait (); }} GlobalData = Count (global data); Generally, most people will give advice instead of waiting / informing, and in return you will use an executor or suggest at least one blocking curve. But both of them that you are currently thinking will need to redesign. 


Comments