Java多线程,交替输出121212

2021-03-27  本文已影响0人  菜的无法无天
package com.imtx.demo;

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

/**
 * @version : 1.0
 * @description: java类作用描述
 * @author: tianwen
 * @create: 2021/3/27 18:52
 **/
public class ThreadTest {

    static Thread a;
    static Thread b;

    static volatile AtomicInteger flag = new AtomicInteger(2);

    static Lock lock = new ReentrantLock();

    public static void main(String[] args) {

        a = new Thread(() -> {
            while (true) {
                lock.lock();
                if (flag.compareAndSet(2, 1)) {
                    System.out.println("1");
                }
                lock.unlock();
            }

        });

        b = new Thread(() -> {
            while (true) {
                lock.lock();
                if (flag.compareAndSet(1, 2)) {
                    System.out.println("2");
                }
                lock.unlock();
            }
        });
        a.start();
        b.start();

    }
}

package com.imtx.demo;

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

/**
 * @version : 1.0
 * @description: java类作用描述
 * @author: tianwen
 * @create: 2021/3/27 18:52
 **/
public class ThreadTest {

    static Thread a;
    static Thread b;

    static Lock lock = new ReentrantLock();

    public static void main(String[] args) {
        Condition condition = lock.newCondition();

        a = new Thread(() -> {
            while (true) {
                lock.lock();
                System.out.println("1");
                condition.signalAll();

                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                lock.unlock();

            }

        });

        b = new Thread(() -> {
            while (true) {
                lock.lock();
                System.out.println("2");
                condition.signalAll();
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                lock.unlock();

            }
        });
        a.start();
        b.start();

    }
}

package com.imtx.demo;

/**
 * @version : 1.0
 * @description: java类作用描述
 * @author: tianwen
 * @create: 2021/3/27 18:52
 **/
public class ThreadTest {

    static class MyThread extends Thread {

        private final Object object;

        private Integer num;

        MyThread(Object object, Integer num) {
            this.object = object;
            this.num = num;
        }

        @Override
        public void run() {
            while (true) {
                synchronized (object) {
                    System.out.println(num);
                    try {
                        object.notify();
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Object object = new Object();
        MyThread a = new MyThread(object, 1);
        MyThread b = new MyThread(object, 2);
        a.start();
        b.start();

    }
}
上一篇下一篇

猜你喜欢

热点阅读