zoukankan      html  css  js  c++  java
  • 两个线程一个输出字母,一个输出数字,交替输出A1B2C3D4E5F6G7。

    第一种方式:

    import java.util.concurrent.CountDownLatch;
    
    public class ConcurrentTest {
        private static CountDownLatch latch= new CountDownLatch(1);
    
        public static void main(String[] args) {
            final Object o = new Object();
            char[] number = "1234567".toCharArray();
            char[] letter = "ABCDEFG".toCharArray();
            new Thread(() -> {
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o) {
                    for (char c : number) {
                        System.out.print(c);
                        try {
                            o.notify();
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    o.notify();
                }
            }, "t1").start();
    
            new Thread(() -> {
                synchronized (o) {
                    for (char c : letter) {
                        System.out.print(c);
                        latch.countDown();
                        try {
                            o.notify();
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    o.notify();
                }
            }, "t2").start();
        }
    }

    第二种方式:
    import java.util.concurrent.CountDownLatch;
    
    public class ConcurrentTest {
        private static volatile boolean task = false;
    
        public static void main(String[] args) {
            final Object o = new Object();
            char[] number = "1234567".toCharArray();
            char[] letter = "ABCDEFG".toCharArray();
            new Thread(() -> {
                synchronized (o) {
                    while (!task) {
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    for (char c : number) {
                        System.out.print(c);
                        try {
                            o.notify();
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    o.notify();
                }
            }, "t1").start();
    
            new Thread(() -> {
                synchronized (o) {
                    for (char c : letter) {
                        System.out.print(c);
                        task = true;
                        try {
                            o.notify();
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    o.notify();
                }
            }, "t2").start();
        }
    }

    第三种方式:
    import java.util.concurrent.locks.LockSupport;
    
    public class ConcurrentTest {
        static Thread t1 = null, t2 = null;
    
        public static void main(String[] args) {
            char[] number = "1234567".toCharArray();
            char[] letter = "ABCDEFG".toCharArray();
            t1 = new Thread(() -> {
                for (char c : number) {
                    LockSupport.park();
                    System.out.print(c);
                    LockSupport.unpark(t2);
    
                }
            }, "t1");
    
            t2 = new Thread(() -> {
                for (char c : letter) {
                    System.out.print(c);
                    LockSupport.unpark(t1);
                    LockSupport.park();
                }
            }, "t2");
            t1.start();
            t2.start();
        }
    }

    第四种方式:

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

    public class ConcurrentTest {
    public static void main(String[] args) {
    char[] number = "1234567".toCharArray();
    char[] letter = "ABCDEFG".toCharArray();
    Lock lock = new ReentrantLock();
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();
    new Thread(() -> {
    try {
    lock.lock();
    condition1.await();
    for (char c : number) {
    System.out.print(c);
    condition2.signal();
    condition1.await();
    }
    condition2.signal();
    } catch (InterruptedException e) {
    e.printStackTrace();
    } finally {
    lock.unlock();
    }
    }, "t1").start();

    new Thread(() -> {
    try {
    lock.lock();
    for (char c : letter) {
    System.out.print(c);
    condition1.signal();
    condition2.await();
    }
    condition1.signal();
    } catch (InterruptedException e) {
    e.printStackTrace();
    } finally {
    lock.unlock();
    }
    }, "t2").start();

    }
    }
  • 相关阅读:
    node学习网站
    UDP 通讯设置超时时间
    Linux下的五个查找命令:grep、find、locate、whereis、which
    apache 2.4设置禁止访问phpmyadmin
    apache 禁止访问根目录
    linux 下 apache启动、停止、重启命令
    Apache2.4使用require指令进行访问控制–允许或限制IP访问/通过User-Agent禁止不友好网络爬虫
    Apache httpd.conf配置文件AllowOverride参数详解
    链接远程mysql
    svn提交后利用hooks自动更新web服务器
  • 原文地址:https://www.cnblogs.com/nginxTest/p/13064682.html
Copyright © 2011-2022 走看看