- synchronized
下面展示一些 内联代码片
。
public class PrintLetter_Synchornize {
private static int count = 0;
private static Object obj = new Object();
public static void createThread(String name, int targetNum) {
new Thread(() -> {
for (int i = 0; i < 10; i++) {
synchronized (obj) {
while (count % 3 == targetNum) {
System.out.println(Thread.currentThread().getName());
count++;
obj.notifyAll();
}
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, name).start();
}
public static void main(String[] args) {
createThread("A", 0);
createThread("B", 1);
createThread("C", 2);
}
}
- join
public class PrintLetter_Join {
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 10; i++) {
Thread a = createThread("A", null);
Thread b = createThread("B", a);
Thread c = createThread("C", b);
a.start();
b.start();
c.start();
c.join();
}
}
public static Thread createThread(String name, Thread prev) {
return new Thread(() -> {
if (prev != null) {
try {
prev.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
} else {
System.out.println(Thread.currentThread().getName());
}
}, name);
}
}
- Lock
public class PrintLetter_Lock {
private static Lock lock = new ReentrantLock();
private static int state = 0;
public static void main(String[] args) {
createThread("A", 0);
createThread("B", 1);
createThread("C", 2);
}
public static void createThread(String name, int targetNum) {
new Thread(() -> {
for (int i = 0; i < 10; ) {
lock.lock();
try {
while (state % 3 == targetNum) {
System.out.println(Thread.currentThread().getName());
state++;
i++;
}
} finally {
lock.unlock();
}
}
}, name).start();
}
}
- Lock + Condition
public class PrintLetter_Condition {
private static Lock lock = new ReentrantLock();
private static Condition conditionA = lock.newCondition();
private static Condition conditionB = lock.newCondition();
private static Condition conditionC = lock.newCondition();
public static void main(String[] args) throws InterruptedException {
PrintLetter_Condition task = new PrintLetter_Condition();
task.createThread("A", conditionA, conditionB);
Thread.sleep(100L);
task.createThread("B", conditionB, conditionC);
task.createThread("C", conditionC, conditionA);
}
public void createThread(String name, Condition await, Condition signal) {
new Thread(() -> {
lock.lock();
try {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName());
signal.signal();
await.await();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}, name).start();
}
}
- Semaphore 信号量
public class PrintLetter_Semaphore {
private static Semaphore semaphoreA = new Semaphore(1);
private static Semaphore semaphoreB = new Semaphore(0);
private static Semaphore semaphoreC = new Semaphore(0);
public static void main(String[] args) throws InterruptedException {
createThread("A", semaphoreA, semaphoreB);
createThread("B", semaphoreB, semaphoreC);
createThread("C", semaphoreC, semaphoreA);
}
private static void createThread(String name, Semaphore acquire, Semaphore release) {
new Thread(() -> {
for (int i = 0; i < 10; i++) {
try {
acquire.acquire();
System.out.println(Thread.currentThread().getName());
release.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, name).start();
}
}