一、使用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. 漂亮的CSS3提交意见输入框样式

    做了个输入框样式,如图: CSS代码如下: <喎�"http://www.2cto.com/kf/ware/vc/" target="_blank" cl ...

  2. Linux下安装nmap扫描工具

    NMAP是一款流行的网络扫描和嗅探工具,被广泛应用在黑客领域做漏洞探测以及安全扫描,更多的nmap是一个好用的网络工具,在生产和开发中也经常用到,主要做端口开放性检测和局域网信息的查看收集等,不同Li ...

  3. 从头认识java-18.2 主要的线程机制(4)-优先级

    这一章节我们来讨论一下多线程的优先级问题. 1.样例: package com.ray.ch17; public class Test { public static void main(String ...

  4. Hadoop:开发机运行spark程序,抛出异常:ERROR Shell: Failed to locate the winutils binary in the hadoop binary path

    问题: windows开发机运行spark程序,抛出异常:ERROR Shell: Failed to locate the winutils binary in the hadoop binary ...

  5. .NET 托管、非托管、本地:这些代码有什么区别?

    http://www.codeguru.com/Csharp/.NET/cpp_managed/article.php/c4871 本文内容 什么是托管代码? 什么是非托管代码? 什么是本地代码? 托 ...

  6. java报错综合

    1.java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I 方法一:这是因为在struts2自带的antlr-2.7.2.jar包的 ...

  7. iOS 里RGB 配色 UIColor colorWithRed

    //比如rgb 色值为73. 148 .230 那么ios 里面要在后面加.0f 再除以255 [bline setBackgroundColor:[UIColor colorWithRed:73.0 ...

  8. JavaScript Array 对象常用方法

    <script type="text/javascript"> //shift:删除原数组的第一项,返回删除元素的值:如果数组为空则返回undefined var ar ...

  9. keytab生成不了

    vim /var/kerberos/krb5kdc/kadm5.acl 将*e改成* /etc/init.d/kadmin restart 重启kadmin

  10. vs2015使用Apache Cordova用JavaScript来访问本地设备的功能,比如摄像头、加速计

    看到下面这张图就代表着我VS2015 跨平台Moblie开发工具安装成功了. 上周安装成功后本想一睹跨平台开发的乐趣,可是一直找不到合适的入口.这周又来捯饬一下结果发现了一个入口.于是来写一个Hell ...