美文网首页
java Thread的wait,notify,wait,sle

java Thread的wait,notify,wait,sle

作者: giveyoume | 来源:发表于2019-01-09 14:48 被阅读0次

    将做工程过程比较重要的一些代码片段做个备份,如下的资料是关于java Thread的wait,notify,wait,sleep简单演示的代码,应该能对各朋友有一些好处。

    package org.he.util;

    public class Test extends Thread {

    Object lock = null;

    boolean notifyFlag = false;

    public Test(Object lock, boolean notifyFlag) {

    this.lock = lock;

    this.notifyFlag = notifyFlag;

    }

    @Override

    public void run() {

    synchronized (lock) {

    System.out.println((notifyFlag == true ? "(notifyThread)" : "") + Thread.currentThread().getName()

    + " hava in partA");

    try {

    if (notifyFlag)

    else

    lock.wait();

    } catch (InterruptedException e) {

    e.printStackTrace();

    }

    System.out.println((notifyFlag == true ? "(notifyThread)" : "") + Thread.currentThread().getName()

    + " hava in partB");

    }

    }

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

    Object lock = new Object();

    new Test(lock, false).start();

    new Test(lock, false).start();

    new Test(lock, false).start();

    Thread.sleep(10000);

    new Test(lock, true).start();

    }

    }

    相关文章

      网友评论

          本文标题:java Thread的wait,notify,wait,sle

          本文链接:https://www.haomeiwen.com/subject/xtcgrqtx.html