[Leetcode] alternate print FooBar

[Problem] we offer a class:

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");
    }
  }
}

Two different threads will share a FooBar example. One thread will call foo () method, another thread will call bar () method.

Please modify the program designed to ensure that "foobar" is output n times.

Example 1 : 
Input: n- = 1 
Output: " foobar " 
explanation: There are two threads are started asynchronously. Wherein a call to foo () method, another call bar () method, " foobar " will be output once.
Example 2 : 
Input: n- = 2 
Output: " foobarfoobar " 
explains: " foobar " is outputted twice.

【answer】

public  class FooBar { 

    Private  int n-;
     // obj lock object 
    Private Object obj;
     // COUNT is equal to 0 to 2% print foo, is printed is equal to 1 bar 
    Private  static  int COUNT = 0 ; 

    public FooBar ( int n-) {
         the this . = n- n-; 
        obj = new new Object (); 
    } 

    public  void foo (the Runnable printFoo) throws InterruptedException {
         for ( int I = 0 ; I <n-; I ++  ) {
            the synchronized (obj) {
                 // COUNT. 1% 2 == bar should be printed, so to sleep, the lock release 
                IF (COUNT% 2 == . 1 ) { 
                    obj.wait (); 
                } 
                // COUNT == 2% 0 or is awakened, print foo 
                printFoo.run (); 
                COUNT ++ ;
                 // print finish foo, should go to print bar across the wake 
                obj.notify (); 
            } 
        } 
    }      

    public  void bar (Runnable printBar) throws {InterruptedException     
         // sleep for 10 ms, to ensure the implementation of the method foo is 
        the 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 new  printFoo();
        printBar bar = new new printBar (); 
        
        // Create a thread anonymous inner classes print foo 
        new new the Thread () {
             public  void RUN () {
                 the try { 
                    fb.foo (foo); 
                } the catch (InterruptedException E) {
                     // the TODO Block the catch-Generated Auto 
                    e.printStackTrace (); 
                } 
            } 
        } .start (); 
        
        // Create a thread anonymous inner classes print bar 
        new new the thread () {
             public  void RUN () {
                 the try {
                    fb.bar(bar);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }.start();
    }
}

 

Guess you like

Origin www.cnblogs.com/zhudingtop/p/11668009.html