zoukankan      html  css  js  c++  java
  • 【Java多线程】Java两个线程轮询打印A1B2C3

    1.synchronized+wait()+notify()方式

    static Object object = new Object();
        public static void main(String[] args) {
            char[] num = "1234567".toCharArray();
            char[] str = "ABCDEFG".toCharArray();
            new Thread(() -> {
                synchronized (object) {
                    for (char c : str) {
                        try {
                            System.out.println(Thread.currentThread().getName()+" = " + c);
                            object.wait();
                            object.notify();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
    
            }, "t1").start();
            new Thread(() -> {
                synchronized (object) {
                    for (char c : num) {
                        try {
                            System.out.println(Thread.currentThread().getName()+" = " + c);
                            object.notify();
                            object.wait();
    
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
    
            }, "t2").start();

    2.Lock+Condition 、await()+signal()

    static Lock lock = new ReentrantLock();
        static Condition condition = lock.newCondition();
        public static void main(String[] args) {
            char[] num = "1234567".toCharArray();
            char[] str = "ABCDEFG".toCharArray();
            new Thread(()->{
                lock.lock();
                try {
                    for (char c : str) {
                        System.out.println(Thread.currentThread().getName()+" = " + c);
                        condition.await();
                        condition.signal();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            },"t1").start();
            new Thread(()->{
                lock.lock();
                try {
                    for (char c : num) {
                        System.out.println(Thread.currentThread().getName()+" = " + c);
                        condition.signal();
                        condition.await();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            },"t2").start();
        }

    3.AtomicInteger原子类

    static AtomicInteger thredNo=new AtomicInteger(1);
        public static void main (String[] args){
            char[] num="1234567".toCharArray();
            char[] str="ABCDEFG".toCharArray();
    
    
            new Thread(()->{
                for (char c : num) {
                    //如果不是1就一直返回空,直到运行打印,打印完之后把原子对象变成2
                    while (thredNo.get()!=1){}
                    System.out.println(c);
                    thredNo.set(2);
                }
    
            },"t1").start();
    
    
            new Thread(()->{
                for (char c : str) {
                    //如果不是2就一直返回空,直到运行打印,打印完之后把原子对象变成1
                    while (thredNo.get()!=2){}
                    System.out.println(c);
                    thredNo.set(1);
                }
    
            },"t1").start();
        }

    4.LockSupport

    static Thread t1 ;
        static Thread t2 ;
        public static void main(String[] args) {
            char[] num = "1234567".toCharArray();
            char[] str = "ABCDEFG".toCharArray();
            t1 = new Thread(()->{
                for (char c : num) {
                    System.out.println(Thread.currentThread().getName()+"::::"+c);
                    java.util.concurrent.locks.LockSupport.unpark(t2);
                    java.util.concurrent.locks.LockSupport.park();
                }
            },"t1");
            t2 = new Thread(()->{
                for (char c : str) {
                    java.util.concurrent.locks.LockSupport.park();
                    System.out.println(Thread.currentThread().getName()+"::::"+c);
                    java.util.concurrent.locks.LockSupport.unpark(t1);
                }
            },"t2");
            t1.start();
            t2.start();
        }
  • 相关阅读:
    数据结构01——线性表
    hdu 6069 Counting Divisors (唯一正整数分解定理+素数筛)
    hdu 6053 TrickGCD (莫比乌斯)
    hdu 1695 GCD(莫比乌斯入门)
    poj 2096 Collecting Bugs (概率dp)
    DC.p4: programming the forwarding plane of a data-center switch
    Packet Transactions: High-level Programming for Line-Rate Switches
    P4: Programming Protocol-Independent Packet Processors
    Improving Network Management with Software Defined Networking
    Are We Ready for SDN? Implementation Challenges for Software-Defined Networks
  • 原文地址:https://www.cnblogs.com/aioe/p/15246534.html
Copyright © 2011-2022 走看看