synchronized 實現(xiàn)
public class ThreadTest {
public boolean flag;
public class OddClass implements Runnable {
public ThreadTest t;
public OddClass(ThreadTest t) {
this.t = t;
}
@Override
public void run() {
int i = 1; //本線程打印奇數(shù),則從1開始
while (i < 100) {
synchronized (t) {
if (!t.flag) {
System.out.println("奇數(shù):" + i);
i += 2;
t.flag = true;
t.notify();
} else {
try {
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
public class EvenClass implements Runnable {
public ThreadTest t;
public EvenClass(ThreadTest t) {
this.t = t;
}
@Override
public void run() {
int i = 2;
while (i <= 100)
synchronized (t) {
if (t.flag) {
System.out.println("偶數(shù):" + i);
i += 2;
t.flag = false;
t.notify();
} else {
try {
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) {
ThreadTest tt = new ThreadTest();
OddClass jiClass = tt.new OddClass(tt);
EvenClass ouClass = tt.new EvenClass(tt);
new Thread(jiClass).start();
new Thread(ouClass).start();
}
}
ReentrantLock 實現(xiàn)
public class ThreadTest2 {
public boolean flag;
private Lock lock = new ReentrantLock();
private final Condition isEven = lock.newCondition();// 偶數(shù)
private final Condition isNotEven = lock.newCondition();// 奇數(shù)
public class OddClass implements Runnable {
public ThreadTest2 t;
public OddClass(ThreadTest2 t) {
this.t = t;
}
@Override
public void run() {
int i = 1; // 本線程打印奇數(shù),則從1開始
while (i < 100) {
lock.lock();
if (!t.flag) {
System.out.println("奇數(shù):" + i);
i += 2;
t.flag = true;
isNotEven.signal();
} else {
try {
isEven.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lock.unlock();
}
}
}
public class EvenClass implements Runnable {
public ThreadTest2 t;
public EvenClass(ThreadTest2 t) {
this.t = t;
}
@Override
public void run() {
int i = 2;
while (i <= 100) {
lock.lock();
if (t.flag) {
System.out.println("偶數(shù):" + i);
i += 2;
t.flag = false;
isEven.signal();
} else {
try {
isNotEven.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lock.unlock();
}
}
}
public static void main(String[] args) {
ThreadTest2 tt = new ThreadTest2();
OddClass jiClass = tt.new OddClass(tt);
EvenClass ouClass = tt.new EvenClass(tt);
new Thread(jiClass).start();
new Thread(ouClass).start();
}
}
運行結(jié)果
奇數(shù):1 偶數(shù):0 奇數(shù):3 偶數(shù):2 奇數(shù):5 偶數(shù):4 奇數(shù):7 偶數(shù):6 奇數(shù):9 偶數(shù):8 奇數(shù):11 偶數(shù):10 奇數(shù):13 偶數(shù):12 奇數(shù):15 偶數(shù):14 奇數(shù):17 偶數(shù):16 奇數(shù):19 偶數(shù):18 奇數(shù):21 偶數(shù):20 奇數(shù):23 偶數(shù):22 奇數(shù):25 偶數(shù):24 奇數(shù):27 偶數(shù):26 奇數(shù):29 偶數(shù):28 奇數(shù):31 偶數(shù):30 奇數(shù):33 偶數(shù):32 奇數(shù):35 偶數(shù):34 奇數(shù):37 偶數(shù):36 奇數(shù):39 偶數(shù):38 奇數(shù):41 偶數(shù):40 奇數(shù):43 偶數(shù):42 奇數(shù):45 偶數(shù):44 奇數(shù):47 偶數(shù):46 奇數(shù):49 偶數(shù):48 奇數(shù):51 偶數(shù):50 奇數(shù):53 偶數(shù):52 奇數(shù):55 偶數(shù):54 奇數(shù):57 偶數(shù):56 奇數(shù):59 偶數(shù):58 奇數(shù):61 偶數(shù):60 奇數(shù):63 偶數(shù):62 奇數(shù):65 偶數(shù):64 奇數(shù):67 偶數(shù):66 奇數(shù):69 偶數(shù):68 奇數(shù):71 偶數(shù):70 奇數(shù):73 偶數(shù):72 奇數(shù):75 偶數(shù):74 奇數(shù):77 偶數(shù):76 奇數(shù):79 偶數(shù):78 奇數(shù):81 偶數(shù):80 奇數(shù):83 偶數(shù):82 奇數(shù):85 偶數(shù):84 奇數(shù):87 偶數(shù):86 奇數(shù):89 偶數(shù):88 奇數(shù):91 偶數(shù):90 奇數(shù):93 偶數(shù):92 奇數(shù):95 偶數(shù):94 奇數(shù):97 偶數(shù):96 奇數(shù):99 偶數(shù):98