1.wait-notifyAll
同步方法
package com.study.demo;
/**
* Created by Administrator on 2019\3\2 0002.
*/
public class Test {
private int seq = 1;
public synchronized void method1() {
try {
while (seq != 1) {
wait();
}
System.out.println(Thread.currentThread().getName() + " method1 run");
seq = 2;
notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void method2() {
try {
while (seq != 2) {
wait();
}
System.out.println(Thread.currentThread().getName() + " method2 run");
seq = 3;
notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void method3() {
try {
while (seq != 3) {
wait();
}
System.out.println(Thread.currentThread().getName() + " method3 run");
seq = 4;
notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Test test = new Test();
new Thread1(test).start();
new Thread2(test).start();
new Thread3(test).start();
}
}
class Thread1 extends Thread {
private Test test;
public Thread1(Test test) {
this.test = test;
}
@Override
public void run() {
test.method1();
}
}
class Thread2 extends Thread {
private Test test;
public Thread2(Test test) {
this.test = test;
}
@Override
public void run() {
test.method2();
}
}
class Thread3 extends Thread {
private Test test;
public Thread3(Test test) {
this.test = test;
}
@Override
public void run() {
test.method3();
}
}
method1 run
method2 run
method3 run
同步代码块
package com.study.demo;
/**
* Created by Administrator on 2019\3\2 0002.
*/
public class Test {
private int seq = 1;
private final Object lock = new Object();
public void method1() {
synchronized (lock) {
try {
while (seq != 1) {
lock.wait();
}
System.out.println(Thread.currentThread().getName() + " method1 run");
seq = 2;
lock.notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void method2() {
synchronized (lock) {
try {
while (seq != 2) {
lock.wait();
}
System.out.println(Thread.currentThread().getName() + " method2 run");
seq = 3;
lock.notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void method3() {
synchronized (lock) {
try {
while (seq != 3) {
lock.wait();
}
System.out.println(Thread.currentThread().getName() + " method3 run");
seq = 4;
lock.notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Test test = new Test();
new Thread1(test).start();
new Thread2(test).start();
new Thread3(test).start();
}
}
class Thread1 extends Thread {
private Test test;
public Thread1(Test test) {
this.test = test;
}
@Override
public void run() {
test.method1();
}
}
class Thread2 extends Thread {
private Test test;
public Thread2(Test test) {
this.test = test;
}
@Override
public void run() {
test.method2();
}
}
class Thread3 extends Thread {
private Test test;
public Thread3(Test test) {
this.test = test;
}
@Override
public void run() {
test.method3();
}
}
2.await-signal
package com.study.demo;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* Created by Administrator on 2019\3\2 0002.
*/
public class Test {
private int seq = 1;
private final ReentrantLock lock = new ReentrantLock();
private final Condition condition1 = lock.newCondition();
private final Condition condition2 = lock.newCondition();
private final Condition condition3 = lock.newCondition();
public void method1() {
lock.lock();
try {
while (seq != 1) {
condition1.await();
}
System.out.println(Thread.currentThread().getName() + " method1 run");
seq = 2;
condition2.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void method2() {
lock.lock();
try {
while (seq != 2) {
condition1.await();
}
System.out.println(Thread.currentThread().getName() + " method2 run");
seq = 3;
condition3.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void method3() {
lock.lock();
try {
while (seq != 3) {
condition3.await();
}
System.out.println(Thread.currentThread().getName() + " method3 run");
seq = 4;
condition1.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
Test test = new Test();
new Thread1(test).start();
new Thread2(test).start();
new Thread3(test).start();
}
}
class Thread1 extends Thread {
private Test test;
public Thread1(Test test) {
this.test = test;
}
@Override
public void run() {
test.method1();
}
}
class Thread2 extends Thread {
private Test test;
public Thread2(Test test) {
this.test = test;
}
@Override
public void run() {
test.method2();
}
}
class Thread3 extends Thread {
private Test test;
public Thread3(Test test) {
this.test = test;
}
@Override
public void run() {
test.method3();
}
}
3.join方法
package com.study.demo;
/**
* Created by Administrator on 2019\3\2 0002.
*/
public class Test {
public static void main(String[] args) throws InterruptedException {
Thread1 thread1 = new Thread1();
thread1.start();
thread1.join();
Thread1 thread2 = new Thread1();
thread2.start();
thread2.join();
Thread1 thread3 = new Thread1();
thread3.start();
thread3.join();
}
}
class Thread1 extends Thread {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " run");
}
}
Thread-0 run
Thread-1 run
Thread-2 run
4.Semaphore信号量
package com.study.demo;
import java.util.concurrent.Semaphore;
/**
* Created by Administrator on 2019\3\2 0002.
*/
public class Test {
private static Semaphore A = new Semaphore(1);
private static Semaphore B = new Semaphore(1);
private static Semaphore C = new Semaphore(1);
public static void main(String[] args) {
ThreadA a = new ThreadA();
ThreadB b = new ThreadB();
ThreadC c = new ThreadC();
try {
B.acquire();
C.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
a.start();
b.start();
c.start();
}
static class ThreadA extends Thread {
@Override
public void run() {
try {
A.acquire();
System.out.println("thread a run");
B.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static class ThreadB extends Thread {
@Override
public void run() {
try {
B.acquire();
System.out.println("thread b run");
C.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static class ThreadC extends Thread {
@Override
public void run() {
try {
C.acquire();
System.out.println("thread c run");
A.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}