public static Object obj1 = new Object();

public static void printAB(){

    Thread t1 = new Thread(() -> {
        while (true){
            synchronized (obj1) {
                System.out.println("T1:A");
                obj1.notify();
                try {
                    Thread.sleep(1000);
                    obj1.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    Thread t2 = new Thread(() -> {
        while (true){
            synchronized (obj1) {
                System.out.println("T2:B");
                obj1.notify();
                try {
                    Thread.sleep(1000);
                    obj1.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });

    t1.start();
    t2.start();

}