Технологии Java
Введение в многопоточное программирование
// Матрицы размера n×n double[][] a, b, c; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { c[i][j] = 0; for (int k = 0; k < n; k++) { c[i][j] += a[i][k] * b[k][j]; } } }
// Матрицы размера n×n /*shared*/ double[][] a, b, c; for (int i = 0; i < n; i++) { // Параллельно for (int j = 0; j < n; j++) { // Параллельно c[i][j] = 0; for (int k = 0; k < n; k++) { c[i][j] += a[i][k] * b[k][j]; } } }
worker[i] { double[] a; // a[i][⋅] double[][] b; // b[⋅][⋅] double[] c; // c[i][⋅] receive a, b from coordinator; for (int j = 0; j < n; j++) { c[j] = 0; for (int k = 0; k < n; k++) { c[j] += a[k] * b[k][j]; } } send c to coordinator; }
coordinator(int i) { double[][] a, b, c; for (int i = 0; i < n; i++) { send a[i], b to worker[i]; } for (int i = 0; i < n; i++) { receive c[i] from worker[i]; } }
worker[i] { double[] a; // a[i][⋅] double[] b; // b[⋅][i] double[] c; // c[i][⋅] receive a, b from coordinator; for (int j = 0; j < n; j++) { // Вычисление с[j] send b to worker[(i + 1) % n]; receive b from worker[(i + n - 1) % n]; } send c to coordinator; }
double integrate(double l, double r) { if (abs(area(l, m) + area(m, r) - area(l, r)) > ε) { return integrate(l, m) + integrate(m, r); } else { return area(l, m) + area(m, r); } } double area(double l, double r) { return (f(l) + f(r)) * (r - l) / 2; }
double integrate(double l, double r) { double m = (l + r) / 2; double la = area(l, m); double ra = area(m, r); if (abs(la + ra - area(l, r)) > ε) { la = integrate(l, m); // Параллельно ra = integrate(m, r); // Параллельно } return la + ra; }
〈statements〉
〈await(C) statements〉
/* shared */ int max = 0; create worker[i] { if (max < a[i]) max = a[i]; }
/* shared */ int max = 0; create worker[i] { 〈if (max < a[i]) max = a[i];〉 }
/* shared */ int max = 0; create worker[i] { if (max < a[i]) { 〈if (max < a[i]) max = a[i];〉 } }
// Создание Thread t = new Thread(() -> { System.out.println("Hello"); }); // Запуск t.start();
// Создание Thread t = new Thread() { public void run() { System.out.println("Hello"); } }; // Запуск t.start();
getState() | isAlive() |
---|---|
NEW | |
RUNNABLE | + |
BLOCKED | + |
WAITING | + |
TIMED_WAITING | + |
TERMINATED |
class Worker implements Runnable { public void run() { try { while (!Thread.interrupted()) { // Полезные действия } } catch (InterruptedException e) { // Поток заканчивает работу } finally { // Восстановление флага прерывания Thread.currentThread().interrupt(); } } }
synchronized (o) { // Получение блокировки … } // Снятие блокировки
public synchronized int getValue() { … }
public int getValue() { synchronized (this) { … } }
class Example { public static synchronized int getValue() { … }
class Example { public static int getValue() { synchronized (Example.class) { … } }
class Queue { private Object data; public void set(Object data) { … } public Object get() { … } }
public void set(Object data) { while (true) { // Активное ожидание synchronized (this) { if (data == null) { this.data = data; break; } } } }
public Object get() { while (true) { // Активное ожидание synchronized (this) { if (data != null) { Object d = data; data = null; return d; } } } }
monitor.unlock() monitor.await() monitor.lock()
public synchronized void set(Object data) throws InterruptedException { while (data != null) { wait(); // Пассивное ожидание } this.data = data; notifyAll(); }
public synchronized Object get() throws InterruptedException { while (data == null) { wait(); // Пассивное ожидание } Object d = data; data = null; notifyAll(); return d; }
if (!queue.isEmpty()) { // Fail Object o = queue.poll(); }
int a = 0;
a = -1;
System.out.println(a);
long a = 0;
a = -1;
System.out.println(a);
int a = 0; int b = 0;
a = 10; b = 2;
System.out.println(a + b);
int a = 0;
a = 1; a = 2;
volatile List<String> list = null;
List<String> l = new ArrayList<>(); l.add("Hello"); list = l;
while (list == null) { } return list.get(0);
public class Singleton { public static volatile Singleton instance; public static Singleton getInstance() { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { instance = new Singleton(); } } } return instance; } }
public void run() { // 0 synchronized (o1) { // 1 o1.notifyAll(); // 2 synchronized (o2) { // 3 try { o2.wait(); // unlock 4, await 5, lock 6 } catch (InterruptedException e) {} } // 7 } // 8 }
public await(Barrier that) { // 0 synchronized (this) { // 1 this.gen++; // 2 this.notify(); // 3 } // 4 synchronized (that) { // 5 while (this.gen > that.gen) { // 6 that.wait(); // unlock 7, await 8, lock 9 } // 10 } // 11 }