import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; class InkrementThread extends Thread { private int member = 0; public InkrementThread(int x) { member = x; } @Override public void run() { int last = member + 10; while (member < last) { //synchronized (InkrementRunnable.intrinsicLock) { member = member + 1; //} System.out.println(this.getName() + ": " + member); } } public int get() { return member; } } class InkrementRunnable extends Object implements Runnable, Comparable { private static int zaehler = 0; private int m; private String name; protected final static Object intrinsicLock = new String(); private final static Lock lock = new ReentrantLock(); public InkrementRunnable(String name) { this.name = name; } public static int getZaehler() { return zaehler; } public int getM() { return m; } public void setM(int m) { if (m < 0) throw new IllegalArgumentException(); this.m = m; } public void main() { System.out.println(name + " arbeitet..."); } public void run() { main(); // benötigt KEINE Synchronisierung: plus(); minus(); for (int i = 0; i < 10; i++) { // Folgendes tut zaehler++ lock.lock(); try { int x = zaehler; System.out.println(name + " liest " + x); x = x + 1; zaehler = x; System.out.println(name + " schreibt " + x); } finally { lock.unlock(); } synchronized(intrinsicLock) { // do sth critical } } } public synchronized void plus() { int y = m; System.out.println("Plus 1"); y = y + 1; m = y; } public void minus() { synchronized(this) { int y = m; System.out.println("Minus 1"); y = y - 1; m = y; } } @Override public int compareTo(Object t) { return 0; } } public class Threads { public static void main(String[] args) { //beispielThreadKlasse(); beispielRunnableKlasse(); } public static void beispielThreadKlasse() { Thread t0 = new InkrementThread(0); InkrementThread t1 = new InkrementThread(10); t0.start(); t1.start(); while (true) { try { t0.join(); t1.join(); break; } catch (InterruptedException e) { } } System.out.println("Ergebnis t0: " + ((InkrementThread)t0).get()); System.out.println("Ergebnis t1: " + t1.get()); for (int i = 1; i <= 10; i++) { System.out.println("main: " + (100 + i)); } } public static void beispielRunnableKlasse() { InkrementRunnable r0 = new InkrementRunnable("r0"); Runnable r1 = new InkrementRunnable("r1"); Thread t0 = new Thread(r0); Thread t1 = new Thread(r1); Thread t2 = new Thread(new InkrementRunnable("r2")); //System.out.println(r0.getM()); //System.out.println(((InkrementRunnable)r1).getM()); t0.start(); t1.start(); t2.start(); r0.plus(); r0.minus(); try { t0.join(); t1.join(); t2.join(); } catch (InterruptedException e) { } System.out.println("Zaehler: " + InkrementRunnable.getZaehler()); // 30 } public static void beispielInnereKlassen() { // Innere Klassen in Methoden: int x = 5; x = 6; final int z = 7; final Lock a = new ReentrantLock(); class Auto extends Thread implements Runnable { @Override public void run() { //int y = x; a.lock(); try { int y = z; y = y + 1; System.out.println(y); } finally { a.unlock(); } } } class Haus implements Runnable { @Override public void run() { } } Thread t4 = new Auto(); //t4.start(); Thread t5 = new Thread(new Haus()); //t5.start(); } public static void beispielAnonymeRunnableUndLambda() { Thread t = new Thread(new Runnable() { @Override public void run() { System.out.println("working..."); System.out.println("w2"); } }); Thread tLambda = new Thread(() -> { System.out.println("working..."); System.out.println("w2"); }); Thread tLambda2 = new Thread(() -> System.out.println("working...")); } }