У меня есть сотовый класс, представляющий одно значение и в ячейке класс swapThread которого запустить метод просто вызвать метод swapValue().Синхронизированные темы поведение
public static void main(String[] args) throws InterruptedException {
Cell c1 = new Cell(15);
Cell c2 = new Cell(23);
Thread t1 = new swapThread(c1, c2);
Thread t2 = new swapThread(c2, c1);
t1.start();
t2.start();
}
класс Сотовый:
class Cell {
private static int counter = 0;
private int value, id;
public Cell(int v) {
value = v;
id = counter++;
}
synchronized int getValue() {
return value;
}
synchronized void setValue(int v) {
value = v;
}
void swapValue(Cell other) {
int t = getValue();
System.out.println("Swapping " + t);
int v = other.getValue();
System.out.println("with " + v);
setValue(v);
other.setValue(t);
System.out.println("Cell is now " + getValue());
System.out.println("Cell was " + other.getValue());
}
}
и класс swapThread:
class swapThread extends Thread {
Cell cell, othercell;
public swapThread(Cell c, Cell oc) {
cell = c;
othercell = oc;
}
public void run() {
cell.swapValue(othercell);
}
}
Обычный выход:
Swapping 15
Swapping 23
with 23
with 15
Cell is now 23
Cell is now 15
Cell was 15
Cell was 23
Я могу просто дождаться, когда thread1 завершится с Thread.join() в основном методе, но есть ли способ избежать этого, изменив синхронизированные методы.
в 'swapValue()', перед сменой, вам нужно сначала получить блокировку на обеих ячейках ('' this' и other') для выполнения операции атомарно. –
Обмен не является атомарным в соответствии с вашей реализацией. –
Есть ли способ решить проблему, не вставляя какие-либо блокирующие объекты? – Nikola