Java多线程4
发布日期:2021-04-30 21:05:56 浏览次数:99 分类:精选文章

本文共 5692 字,大约阅读时间需要 18 分钟。

Java???4??????????

?Java???????????????????????????????????????????????????????????????????????????????????????

????????????

??????????????????????????????????????????????????????????????????????????????????

?????

??????????????????????????????????????????????

public class ZiYuan {
private Object object[] = new Object[1];
private int num = 1;
private static final Object loc = new Object();
public void add() {
synchronized (loc) {
while (object[0] != null) {
loc.wait();
}
object[0] = "?" + num;
System.out.println(Thread.currentThread().getName() + "????????" + object[0]);
num++;
loc.notifyAll();
}
}
public void delete() {
synchronized (loc) {
while (object[0] == null) {
loc.wait();
}
System.out.println(Thread.currentThread().getName() + "??????" + object[0]);
object[0] = null;
loc.notifyAll();
}
}
}

?????

???????????????????????????????

public class ShengChan implements Runnable {
private ZiYuan ziyuan;
public ShengChan(ZiYuan ziYuan) {
this.ziyuan = ziYuan;
}
@Override
public void run() {
while (true) {
try {
ziYuan.add();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

?????

???????????????????????????????

public class XiaoFei implements Runnable {
private ZiYuan ziyuan;
public XiaoFei(ZiYuan ziYuan) {
this.ziyuan = ziYuan;
}
@Override
public void run() {
while (true) {
try {
ziYuan.delete();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

????

?????????????????????

public class TestMain {
public static void main(String[] args) {
ZiYuan yuan = new ZiYuan();
ShengChan sc = new ShengChan(yuan);
XiaoFei xf = new XiaoFei(yuan);
Thread scThread = new Thread(sc);
scThread.setName("?????");
Thread xfThread = new Thread(xf);
xfThread.setName("?????");
scThread.start();
xfThread.start();
}
}

????

?????????????????

  • ???????????????????????????????????
  • ??synchronized????????????????????synchronized????????????????
  • ????

    ?????????????Java??Lock???Condition?????????????????/?????

    Lock???Condition??

  • Lock????????????????????????synchronized????
  • Condition??????await()?signal()?signalAll()???????????????????
  • ?????????

    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    import java.util.concurrent.locks.Condition;
    public class ZiYuan {
    private Object object[] = new Object[1];
    private int num = 1;
    private static final Lock lock = new ReentrantLock();
    private static final Condition sCondition = lock.newCondition();
    private static final Condition xfCondition = lock.newCondition();
    public void add() {
    try {
    lock.lock();
    try {
    while (object[0] != null) {
    sCondition.await();
    }
    object[0] = "?" + num;
    System.out.println(Thread.currentThread().getName() + "????????" + object[0]);
    num++;
    xfCondition.signal();
    } catch (InterruptedException e) {
    e.printStackTrace();
    } finally {
    lock.unlock();
    }
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    public void delete() {
    try {
    lock.lock();
    try {
    while (object[0] == null) {
    xfCondition.await();
    }
    System.out.println(Thread.currentThread().getName() + "??????" + object[0]);
    object[0] = null;
    sCondition.signal();
    } catch (InterruptedException e) {
    e.printStackTrace();
    } finally {
    lock.unlock();
    }
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }

    ?????????

    public class ShengChan implements Runnable {
    private ZiYuan ziyuan;
    public ShengChan(ZiYuan ziYuan) {
    this.ziyuan = ziYuan;
    }
    @Override
    public void run() {
    while (true) {
    try {
    ziyuan.add();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }

    ?????????

    public class XiaoFei implements Runnable {
    private ZiYuan ziyuan;
    public XiaoFei(ZiYuan ziYuan) {
    this.ziyuan = ziYuan;
    }
    @Override
    public void run() {
    while (true) {
    try {
    ziyuan.delete();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }

    ????

    public class TestMain {
    public static void main(String[] args) {
    ZiYuan yuan = new ZiYuan();
    ShengChan sc = new ShengChan(yuan);
    XiaoFei xf = new XiaoFei(yuan);
    Thread scThread = new Thread(sc);
    scThread.setName("?????");
    Thread xfThread = new Thread(xf);
    xfThread.setName("?????");
    scThread.start();
    xfThread.start();
    }
    }

    ????

  • ???????Condition???await()?signal()????????????????????????????????????????????
  • ???????Lock??????????????????????????????????????
  • ????????????Condition?????????????????????????????????????????????
  • ????Lock?Condition???????????????????????????????????????????????????????

    上一篇:【Java6】this/super/继承,抽象
    下一篇:Windows10下Zookeeper和kafka安装及配置

    发表评论

    最新留言

    路过,博主的博客真漂亮。。
    [***.116.15.85]2026年05月24日 17时07分33秒