Use Lock class and LockSupport class to achieve alternate thread printout

1. Use the Lock class

package 双线程循环打印ABAB;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author sz
 * @DATE 2022/2/10  11:40
 */
public class LockTest {

    public volatile static String word = "A";
    public volatile static int num = 0;
    public static Lock lock = new ReentrantLock(false);

    public static void main(String[] args) {

        new Thread(() -> {
            while (num<100) {
                lock.lock();
                try {
                    while (num%2==0) {
                        System.out.println("A  : "+num);
                        ++num;
                    }
                } finally {
                    lock.unlock();
                }
            }
        }).start();

        new Thread(() -> {
            while (num<100) {
                lock.lock();
                try {
                    while (num%2!=0) {
                        System.out.println("B  : "+num);
                        ++num;
                    }
                } finally {
                    lock.unlock();
                }
            }
        }).start();


    }

}

2. Use the LockSupport class

package 双线程循环打印ABAB;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;

/**
 * @author sz
 * @DATE 2022/2/10  15:36
 */
public class LockSupportTest {

    public static volatile String name = "A";
    Runnable target;
    public static volatile Thread threadA = new Thread();
    public static volatile Thread threadB = new Thread();


    public static void main(String[] args) {

        threadA = new Thread(() -> {
                while (true){
                    if (name.equals("A")){
                        System.out.println("A  2秒后打印B");
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        name="B";
                        //给另外一个线程许可证
                        LockSupport.unpark(threadB);
                    }else {
                        //禁止当前线程调度资源
                        LockSupport.park();
                    }
                }
        });

        threadB = new Thread(() -> {
            while (true){
                if (name.equals("B")){
                    System.out.println("B  2秒后打印A");
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    name="A";
                    //给另外一个线程许可证
                    LockSupport.unpark(threadA);
                }else {
                    //禁止当前线程调度资源
                    LockSupport.park();
                }
            }
        });

        threadA.start();
        threadB.start();


    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324108472&siteId=291194637