package googleTest;
public class ThreadTest {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new Runner("A"));
Thread t2 = new Thread(new Runner("B"));
Thread t3 = new Thread(new Runner("C"));
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();
}
}
class Runner implements Runnable{
public String name;
Runner(String name)
{
this.name=name;
}
@Override
public void run() {
System.out.println(name+"");
}
}
lock时注意unlock就可以啦import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class ThreadTest3 { private static Lock lock=new ReentrantLock(); private static int state = 0; static class ThreadA extends Thread { @Override public void run() { lock.lock(); if (state % 3 == 0) { System.out.println("A"); state++; } lock.unlock(); } } static class ThreadB extends Thread { @Override public void run() { lock.lock(); if (state % 3 == 1) { System.out.println("B"); state++; } lock.unlock(); } } static class ThreadC extends Thread { @Override public void run() { lock.lock(); if (state % 3 == 2) { System.out.println("C"); state++; } lock.unlock(); } } public static void main(String[] args) { new ThreadA().start(); new ThreadB().start(); new ThreadC().start(); } }
就拿生产者消费者模式来说,当仓库满了的时候,又再执行到 生产者 线程的时候,会把 该 生产者 线程进行阻塞,再唤起一个线程.
但是此时唤醒的是消费者线程还是生产者线程,是未知的。如果再次唤醒的还是生产者线程,那么还需要把它进行阻塞,再唤起一个线程,再此循环,直到唤起的是消费者线程。这样就可能存在 时间或者资源上的浪费,
所以说 有了Condition 这个东西。Condition 用 await() 代替了 Object 的 wait() 方法,用 signal() 方法代替 notify() 方法。注意:Condition 是被绑定到 Lock 中,要创建一个 Lock 的 Condition 必须用 newCondition() 方法。
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ThreadTest2 {
private static Lock lock = new ReentrantLock();
private static int count = 0;
private static Condition A = lock.newCondition();
private static Condition B = lock.newCondition();
private static Condition C = lock.newCondition();
static class ThreadA extends Thread {
@Override
public void run() {
lock.lock();
try {
for (int i = 0; i < 10; i++) {
while (count % 3 != 0)
A.await(); // 会释放lock锁
System.out.print("A");
count++;
B.signal(); // 唤醒相应线程
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
static class ThreadB extends Thread {
@Override
public void run() {
lock.lock();
try {
for (int i = 0; i < 10; i++) {
while (count % 3 != 1)
B.await();
System.out.print("B");
count++;
C.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
static class ThreadC extends Thread {
@Override
public void run() {
lock.lock();
try {
for (int i = 0; i < 10; i++) {
while (count % 3 != 2)
C.await();
System.out.println("C");
count++;
A.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
public static void main(String[] args) throws InterruptedException {
new ThreadA().start();
new ThreadB().start();
ThreadC threadC = new ThreadC();
threadC.start();
threadC.join();
System.out.println(count);
}
}
import java.util.concurrent.Semaphore;
public class ThreadTest4 {
private static Semaphore A = new Semaphore(1);
private static Semaphore B = new Semaphore(1);
private static Semaphore C = new Semaphore(1);
static class ThreadA extends Thread {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
A.acquire();
System.out.print("A");
B.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static class ThreadB extends Thread {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
B.acquire();
System.out.print("B");
C.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static class ThreadC extends Thread {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
C.acquire();
System.out.println("C");
A.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
B.acquire(); C.acquire(); // 开始只有A可以获取, BC都不可以获取, 保证了A最先执行
new ThreadA().start();
new ThreadB().start();
new ThreadC().start();
}
}