美文网首页
Thread.sleep和Object.notify

Thread.sleep和Object.notify

作者: sunnyzhu92 | 来源:发表于2018-04-10 12:00 被阅读0次

    public class SleepWaitTest extends Thread {
    @Override
    public void run() {
    synchronized (this) {
    System.out.println(Thread.currentThread().getName() + ":" + System.currentTimeMillis());
    for (int i=0;i< 100;i++) {
    if (i<99) {
    System.out.println(Thread.currentThread().getName() + ":" + i);
    try {
    if (Thread.currentThread().getName().equals("Thread-1") && i==10) {
    wait(1L);
    }
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    System.out.println(Thread.currentThread().getName() + ":" + System.currentTimeMillis());
    }
    }

    public static void main(String[] args) throws InterruptedException {
        SleepWaitTest sleepWaitTest = new SleepWaitTest();
        new Thread(sleepWaitTest).start();
        new Thread(sleepWaitTest).start();
    
    }
    

    }

    相关文章

      网友评论

          本文标题:Thread.sleep和Object.notify

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