zoukankan      html  css  js  c++  java
  • 两线程交替打印数字

    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();//这里没有释放锁

    }

    }

    }

    }

  • 相关阅读:
    tomcat---基本知识点解读;配置文件详解
    nginx常用配置参数介绍-upstream
    nginx配置文件-详解
    nginx简单介绍
    GCC编译已经引入math.h文件提示cos,sin等函数未定义
    进程与进程间通信(3)
    进程与进程间通信(1)
    进程与进程间通信(2)
    primer_c++_迭代器
    primer_C++_3.5 数组
  • 原文地址:https://www.cnblogs.com/fanguangdexiaoyuer/p/6927581.html
Copyright © 2011-2022 走看看