一、使用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()

Java交替打印两个字符串的更多相关文章

  1. java string截取两个字符串之间的值

    java string截取两个字符串之间的值 import java.util.regex.Matcher; import java.util.regex.Pattern; public class ...

  2. Java 中如何计算两个字符串时间之间的时间差?(单位为分钟)

    Java 中如何计算两个字符串时间之间的时间差?(单位为分钟) import java.text.DateFormat; import java.text.ParseException; import ...

  3. Java-Runoob-高级教程-实例-字符串:10. Java 实例 - 测试两个字符串区域是否相等-uncheck

    ylbtech-Java-Runoob-高级教程-实例-字符串:10. Java 实例 - 测试两个字符串区域是否相等 1.返回顶部 1. Java 实例 - 测试两个字符串区域是否相等  Java ...

  4. java 算法之 两个字符串中最大相同的子串

    public class String_intern { public static void main(String[] args) { String old="aaaaabc1" ...

  5. java中判断两个字符串是否相等的问题

    我最近刚学java,今天编程的时候就遇到一个棘手的问题,就是关于判断两个字符串是否相等的问题.在编程中,通常比较两个字符串是否相同的表达式是“==”,但在java中不能这么写.在java中,用的是eq ...

  6. java操作对比两个字符串,将差异数据提取出来

    记录瞬间 在实际的工作中,需要解决生成两次字符串结果进行对比的问题,将存在差异的字符串直接给出来. 当然,前提是需要将对比的两次结果,进行前期处理 比如: a_str = "@com/ene ...

  7. 用Java编程找到两个字符串中共有的字符

    这道题的算法思想是把字符串1中的每个字符与字符串2中的每个字符进行比较,遇到共同拥有的字符,放入另一个数组中,最后顺序输出即可 但是这道题的难点在于怎么排除重复的字符 public class bot ...

  8. java例题_46 两个字符串拼接问题!

    1 /*46 [程序 46 字符串连接] 2 题目:两个字符串连接程序,将两个字符串拼接在一起 3 */ 4 5 /*分析 6 * 两个字符串的拼接方法 7 * concat方式 8 * 当两个量都为 ...

  9. 【Java】获取两个字符串中最大相同子串

    题目 获取两个字符串中最大相同子串 前提 两个字符串中只有一个最大相同子串 解决方案 public class StringDemo { public static void main(String[ ...

随机推荐

  1. iOS开发-消息通知机制(NSNotification和NSNotificationCenter)

    iOS中委托模式和消息机制基本上开发中用到的比较多,一般最开始页面传值通过委托实现的比较多,类之间的传值用到的比较多,不过委托相对来说只能是一对一,比如说页面A跳转到页面B,页面的B的值改变要映射到页 ...

  2. Set Matrix Zeroes leetcode java

    题目: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cl ...

  3. 混合开发 Hybird Cordova PhoneGap web 跨平台 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  4. 【Scala】Scala-Map使用方法

    Scala-Map使用方法 scala map put_百度搜索 Scala中的Map使用例子 - CSDN博客 How to populate java.util.HashMap on the fl ...

  5. 【R】函数-数学函数

  6. 性能测试vs负载测试vs压力测试

    下面我们主要介绍性能测试.负载测试和压力测试. 效率作为ISO 9126内部和外部质量的重要质量属性之一,其含义是在规定条件下,相对于所用的资源的数量,软件产品可提供适当性能的能力.资源可能包括其他软 ...

  7. Eclipse技术: 项目文件中过滤.o文件

    1. 右建项目 -> Properties. 2. 增加过滤规则

  8. VC++ MFC应用程序项目文件2.cpp

    //GameServer.cpp: 定义应用程序的入口点. // #include "stdafx.h" #include "GameServer.h" #de ...

  9. [Functional Programming] Arrow contramap vs map and promap

    In previous post, Arrow Functor with contramap, we have seen how to opreating on params before we in ...

  10. iOS9 Error Domain=NSURLErrorDomain Code=-1022 App Transport Security (ATS)

    iOS 9在HTTP 访问时会出错  iOS9 Error Domain=NSURLErrorDomain Code=-1022 这时需要修改info.plist 文件 在Info.plist中添加N ...