/**
 * ReentrantLock是独占锁
 * Lock锁的使用,把锁和要用锁同步的代码放在一起,这里就是放在Printer类中了 
   *     获取到锁后,最后要在finally代码块中手动释放锁
 */
public class LockTest {

    public static void main(String[] args) {

        Printer printer = new LockTest().new Printer();

        new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    printer.print("zhangsan33953");
                }

            };
        }.start();

        new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    printer.print("lisi27965");
                }
            };
        }.start();
    }

    class Printer {

        private Lock lock = new ReentrantLock();//默认是非公平锁

        public void print(String name) {
            lock.lock(); // 获取锁 , 获取不到会阻塞
            try {
                
                int len = name.length();
                for (int i = 0; i < len; i++) {
                    System.out.print(name.charAt(i));
                }
                System.out.println();
                
            } finally {
                lock.unlock(); // 释放锁
            }
        }
    }

}

 

相关文章:

  • 2022-12-23
  • 2022-01-04
  • 2021-09-28
  • 2022-01-05
  • 2021-05-17
  • 2021-07-27
  • 2022-01-18
  • 2022-02-05
猜你喜欢
  • 2021-05-15
  • 2021-04-17
  • 2021-08-08
  • 2022-12-23
  • 2021-10-01
相关资源
相似解决方案