package com.mortals.xhx.base;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author: zxfei
 * @date: 2022/8/9 9:49
 * @description:
 **/
public class visit implements Runnable {
    //private boolean someone = false;

    private AtomicBoolean someone = new AtomicBoolean();

    @Override
    public void run() {
        if (someone.compareAndSet(false, true)) {
            try {
                Thread.sleep(1000);
                //someone = true;
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()
                    + " i am coming");
            System.out.println(Thread.currentThread().getName()
                    + " i have to leaving");
            //someone = false;
            someone.set(false);
        } else {
            System.out.println(Thread.currentThread().getName()
                    + " i can't visit the house");
        }
    }

    public static void main(String[] args) {
        visit st = new visit();
        new Thread(st, "thread_1").start();
        new Thread(st, "thread_2").start();
    }
}