zoukankan      html  css  js  c++  java
  • 【Leetcode】交替打印FooBar

    【问题】我们提供一个类:

    class FooBar {
      public void foo() {
        for (int i = 0; i < n; i++) {
          print("foo");
        }
      }
    
      public void bar() {
        for (int i = 0; i < n; i++) {
          print("bar");
        }
      }
    }

    两个不同的线程将会共用一个 FooBar 实例。其中一个线程将会调用 foo() 方法,另一个线程将会调用 bar() 方法。

    请设计修改程序,以确保 “foobar” 被输出 n 次。

    示例 1:
    输入: n = 1
    输出: "foobar"
    解释: 这里有两个线程被异步启动。其中一个调用 foo() 方法, 另一个调用 bar() 方法,"foobar" 将被输出一次。
    示例 2:
    输入: n = 2
    输出: "foobarfoobar"
    解释: "foobar" 将被输出两次。

    【题解】

    public class FooBar{
    
        private int n;
        //obj为对象锁
        private Object obj;
        //count % 2 等于 0 就打印foo,等于1就打印bar
        private static int count = 0;
    
        public FooBar(int n) {
            this.n = n;
            obj = new Object();
        }
    
        public void foo(Runnable printFoo) throws InterruptedException {
            for(int i = 0; i < n; i++) {
                synchronized(obj) {
                    //count % 2 == 1本应该是打印bar的,所以就休眠,释放锁
                    if(count % 2 == 1) {
                        obj.wait();
                    }
                    //count % 2 == 0 或者是被唤醒了,就打印foo
                    printFoo.run();
                    count++;
                    //打印完foo,应该去唤醒对面打印bar了
                    obj.notify();
                }
            }
        }     
    
        public void bar(Runnable printBar) throws InterruptedException {     
            //休眠10毫秒,确保是foo方法先执行
            Thread.sleep(10);
            for(int i = 0; i < n; i++) {
                synchronized(obj) {
                    if(count % 2 == 0) {
                        obj.wait();
                    }
                    printBar.run();
                    count++;
                    obj.notify();
                }
            }
        }  
    
        //代码测试
        public static void main(String[] args) {
            FooBar fb = new FooBar(3);
            printFoo foo = new printFoo();
            printBar bar = new printBar();
            
            //用匿名内部类创建一个线程打印foo
            new Thread() {
                public void run() {
                    try {
                        fb.foo(foo);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }.start();
            
            //用匿名内部类创建一个线程打印bar
            new Thread() {
                public void run() {
                    try {
                        fb.bar(bar);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }.start();
        }
    }
  • 相关阅读:
    IIS6的http.ini伪静态规则转换为IIS7伪静态规则的方法
    php ecshop 二级域名切换跳转时session不同步,解决session无法共享同步导致无法登陆的问题
    ECSHOP登录自动退出问题解决
    aspcms 分享到微信显示找不到模版的解决办法!
    ASPCMS内容调用去掉html标签
    凡是出现了问题,都是有原因存在的。
    spring下载地址
    比较好的资源
    xp系统下,安装github时,需要下载的补丁
    联合更新语句,根据photo中的数量更新相册表Num列
  • 原文地址:https://www.cnblogs.com/zhudingtop/p/11668009.html
Copyright © 2011-2022 走看看