zoukankan      html  css  js  c++  java
  • Java交替打印两个字符串

    一、使用volatile关键字

    public class Main {
    volatile int x = 0;
    
    Main() {
        new Thread(() -> {
            while (x < 100) {
                while (x % 2 == 0) ;
                System.out.print("A");
                x++;
            }
        }).start();
        new Thread(() -> {
            while (x < 100) {
                while (x % 2 == 1) ;
                System.out.print("B");
                x++;
            }
        }).start();
    }
    
    public static void main(String[] args) {
        new Main();
    }
    }
    

    二、使用atomicInteger

    import java.util.concurrent.atomic.AtomicInteger;
    
    public class Main {
    
    Main() {
        final AtomicInteger x = new AtomicInteger(0);
        new Thread(() -> {
            while (x.get() < 100) {
                while (x.get() % 2 == 0) ;
                System.out.print("A");
                x.incrementAndGet();
            }
        }).start();
        new Thread(() -> {
            while (x.get() < 100) {
                while (x.get() % 2 == 1) ;
                System.out.print("B");
                x.incrementAndGet();
            }
        }).start();
    }
    
    public static void main(String[] args) {
        new Main();
    }
    }
    

    三、使用锁

    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class Main {
    Lock x = new ReentrantLock();
    int index = 0;
    
    Main() {
        new Thread(() -> {
            boolean over = false;
            while (!over) {
                if (index % 2 == 0) {
                    x.lock();
                    if (index % 2 == 0) {
                        System.out.print("A");
                        index++;
                    }
                    over = index > 100;
                    x.unlock();
                }
            }
        }).start();
        new Thread(() -> {
            boolean over = false;
            while (!over) {
                if (index % 2 == 1) {
                    x.lock();
                    if (index % 2 == 1) {
                        System.out.print("B");
                        index++;
                    }
                    over = index > 100;
                    x.unlock();
                }
            }
        }).start();
    }
    
    public static void main(String[] args) {
        new Main();
    }
    }
    

    四、使用synchronized关键字

    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class Main {
    int index = 0;
    
    Main() {
        new Thread(() -> {
            boolean over = false;
            while (!over) {
                if (index % 2 == 0) {
                    synchronized (this) {
                        if (index % 2 == 0) {
                            System.out.print("A");
                            index++;
                        }
                        over = index > 100;
                    }
                }
            }
        }).start();
        new Thread(() -> {
            boolean over = false;
            while (!over) {
                if (index % 2 == 1) {
                    synchronized (this) {
                        if (index % 2 == 1) {
                            System.out.print("B");
                            index++;
                        }
                        over = index > 100;
                    }
                }
            }
        }).start();
    }
    
    public static void main(String[] args) {
        new Main();
    }
    }
    

    五、使用Python

    JavaScript是单线程的,Python可以实现多线程,但是Python的多线程不能充分利用多核。
    这就相当于Python天生就不需要volatile。

    a = "床前明月光疑是地上霜"
    from threading import Thread
    
    index = 0
    
    
    def one():
        global index
        while index < len(a):
            while index % 2 == 0:
                pass
            if index >= len(a): break
            print(a[index])
            index += 1
    
    
    def two():
        global index
        while index < len(a):
            while index % 2 == 1:
                pass
            if index >= len(a): break
            print(a[index])
            index += 1
    
    
    Thread(target=one).start()
    Thread(target=two).start()
    
    
  • 相关阅读:
    使用pymouse模块时候报错No module named 'windows'
    解决PIL透明的图片放在新图片上报错
    解决PIL切圆形图片存在锯齿
    常见金融术语-帮助更好的理解金融业务需求
    FastJson序列化时过滤字段(属性)的方法总结
    数据库事务4种隔离级别及7种传播行为
    硬件网络接口规范
    「题解」P5906 【模板】回滚莫队&不删除莫队
    「学习笔记」优美的暴力——莫队
    2017 NOIp提高组 DAY2 试做
  • 原文地址:https://www.cnblogs.com/weiyinfu/p/9511289.html
Copyright © 2011-2022 走看看