У меня есть объект Rotor, у которого есть цель Speed и currentSpeed. Каждый пытается изменить его currentSpeed, чтобы соответствовать набору goalSpeed. У меня 4 из этих роторов, работающих с 4 отдельными потоками. Периодически каждый контроллер получает новую контрольную точку.Синхронизировано между экземплярами объекта
Когда я пытаюсь в каждом Роторе изменить его currentSpeed, я не могу превышать сумму всей текущей скорости ротора, чтобы превысить значение X. sum (currentSpeed (Rotor1) + ... + currentSpeed (Rotor2))!> X.
Вот моя проблема: когда я проверяю, могу ли увеличить текущую скорость ротора, я делаю инструкцию if на сумма скоростей. Однако возможно, что сразу после этой проверки, поскольку каждый ротор представляет собой отдельный поток, который другой изменяет свое значение. Поэтому моя проверка в другом потоке уже недействительна. Как я могу убедиться, что, пока я нахожусь в методе setNewSpeed () одного ротора, никакой другой ротор не изменит его текущую скорость?
class Rotor implements Runnable {
private int id;
private int goalSpeed;
private int currentSpeed;
private Controller controller;
private int Y;
private int failedAttempts;
private int successAttempts;
private int maxSpeed;
public int getSuccessAttempts() {
return successAttempts;
}
public void setSuccessAttempts(int successAttempts) {
this.successAttempts = successAttempts;
}
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
public int getFailedAttempts() {
return failedAttempts;
}
public Rotor(Controller c, int Y, int id){
this.controller = c;
this.Y = Y;
this.id = id;
this.currentSpeed = 0;
this.failedAttempts = 0;
this.goalSpeed = 0;
this.maxSpeed = 0;
this.successAttempts = 0;
}
synchronized public void setGoalSpeed(int s){
this.goalSpeed = s;
}
public int getCurrentSpeed(){
return currentSpeed;
}
synchronized private void setNewSpeed(){
int currentDrain = 0;
for(Rotor r : controller.getRotors()){
currentDrain = currentDrain + r.getCurrentSpeed();
}
if((currentDrain + (goalSpeed - currentSpeed)) > 20){
//we cannot increase by total amount because drain too high
System.out.println("failed");
this.failedAttempts++;
currentSpeed = currentSpeed + (20 - currentDrain);
System.out.println("currentSpeed:" + currentSpeed);
} else {
System.out.println("success");
successAttempts++;
currentSpeed = goalSpeed;
}
// System.out.println("goalSpeed:" + goalSpeed);
// System.out.println("currentDrain:" + currentDrain);
}
public void run() {
try {
while(true){
setNewSpeed();
if(currentSpeed > maxSpeed){
maxSpeed = currentSpeed;
}
Thread.sleep(Y);
}
} catch (InterruptedException e) {
System.out.println("Rotor " + id + ": checks=" + (int)(successAttempts + failedAttempts) + ", success rate=" + successAttempts + ", failedAttempts=" + failedAttempts + ", max=" + maxSpeed);
}
}
}
Он не может получить доступ к свойствам экземпляра из статического метода. – Kayaman
@ Kayaman Однако он может передать экземпляр в параметр, а затем обратиться к приватным методам или даже к частным членам переданного экземпляра. –
Конечно, если он переключит дизайн вокруг. – Kayaman