两线程交替打印数字

import java.util.concurrent.atomic.AtomicInteger;

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.ReentrantLock;

 

public class Test {

 

private static ReentrantLock lock = new ReentrantLock();

 

private static Condition condition1 = lock.newCondition();

 

private static Condition condition2 = lock.newCondition();

 

public static AtomicInteger number = new AtomicInteger(1);

 

public static void main(String[] args) throws Exception {

 

ZZ zz = new ZZ(lock, condition1, condition2);

SS ss = new SS(lock, condition1, condition2);

zz.start();

ss.start();

 

zz.join();

ss.join();

}

}

 

class ZZ extends Thread {

private ReentrantLock lock;

 

private Condition condition1;

 

private Condition condition2;

 

public ZZ(ReentrantLock lock, Condition condition1, Condition condition2) {

this.lock = lock;

this.condition1 = condition1;

this.condition2 = condition2;

}

 

public void run() {

while (Test.number.intValue() < 10) {

lock.lock();

try {

if (Test.number.intValue() % 2 == 1) {

System.out.print(Test.number.intValue() + ",");

Test.number.incrementAndGet();

condition2.signal();

} else {

condition1.await();

}

} catch (Exception e) {

e.printStackTrace();

} finally {

lock.unlock();

}

}

}

}

 

class SS extends Thread {

private ReentrantLock lock;

 

private Condition condition1;

 

private Condition condition2;

 

public SS(ReentrantLock lock, Condition condition1, Condition condition2) {

this.lock = lock;

this.condition1 = condition1;

this.condition2 = condition2;

}

 

public void run() {

 

while (Test.number.intValue() <= 10) {

lock.lock();

try {

if (Test.number.intValue() % 2 == 0) {

System.out.print(Test.number.intValue() + ".");

Test.number.incrementAndGet();

condition1.signal();

} else {

condition2.await();

}

} catch (Exception e) {

e.printStackTrace();

} finally {

lock.unlock();

}

}

}

}

 

 

 

import java.util.concurrent.atomic.AtomicInteger;

 

public class Test2 {

 

private static Object obj1 = new Object();

 

public static AtomicInteger number = new AtomicInteger(1);

 

public static void main(String[] args) throws Exception {

 

ZZ1 zz = new ZZ1(obj1);

SS1 ss = new SS1(obj1);

zz.start();

ss.start();

 

zz.join();

ss.join();

}

}

 

class ZZ1 extends Thread {

 

private Object obj1;

 

public ZZ1(Object obj1) {

this.obj1 = obj1;

}

 

public void run() {

while (Test2.number.intValue() < 10) {

synchronized (obj1) {

if (Test2.number.intValue() % 2 == 0) {

try {

obj1.wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

 

System.out.print(Test2.number.intValue() + ",");

Test2.number.incrementAndGet();

obj1.notify();//这里没有释放锁

}

}

}

}

 

class SS1 extends Thread {

 

private Object obj1;

 

 

public SS1(Object obj1) {

this.obj1 = obj1;

}

 

public void run() {

while (Test2.number.intValue() <= 10) {

synchronized (obj1) {

if (Test2.number.intValue() % 2 == 1) {

try {

obj1.wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

 

System.out.print(Test2.number.intValue() + ".");

Test2.number.incrementAndGet();

obj1.notify();//这里没有释放锁

}

}

}

}

转载于:https://www.cnblogs.com/fanguangdexiaoyuer/p/6927581.html

你可能感兴趣的:(两线程交替打印数字)