一、使用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. 配置Tomcat apr运行模式

    tomcat中一共有三种运行模式,分别是:bio,nio,apr bio是阻塞式IO操作,使用的是传统的java i/o处理方式,对于每一个请求都要创建一个线程来进行处理,所以开销较大不适合处理高并发 ...

  2. IOS 沙盒与清除缓存

    SandBox,沙盒机制,是一种安全体系.我们所开发的每一个应用程序在设备上会有一个对应的沙盒文件夹,当前的程序只能在自己的沙盒文件夹中读取文件,不能访问其他应用程序的沙盒.在项目中添加的所有非代码的 ...

  3. Kaggle网站流量预测任务第一名解决方案:从模型到代码详解时序预测

    Kaggle网站流量预测任务第一名解决方案:从模型到代码详解时序预测 2017年12月13日 17:39:11 机器之心V 阅读数:5931   近日,Artur Suilin 等人发布了 Kaggl ...

  4. 主机无法访问虚拟机的apache解决办法

    1.前言 今天学习搭建wordpress,apache服务器安装在虚拟机的Centos上.配置好以后,发现在虚拟机上可以访问,但在windows主机上不能访问.于是百度.google一下,终于解决问题 ...

  5. 在javascript中substr和substring的区别是什么

      1.substring 用于提取字符串中介于两个指定下标之间的字符 substring(start,end) 开始和结束的位置,从零开始的索引 substring 方法返回的子串包括 start ...

  6. 【Android界面实现】使用PagerTabStrip实现有滑动标签的Viewpager

    在ViewPager这样的能够滑动的控件上,总是有非常多的文章能够做.上次的文章,我们实现了一个自己定义的ViewPager的指示器,这篇文章,我们主要是想利用Android自带的控件,实现一个指示器 ...

  7. PAT 1065 1066 1067 1068

    pat 1065 A+B and C                                          主要是注意一下加法溢出的情况,不要试图使用double,因为它的精度是15~16 ...

  8. Python在线dlib库地址

    一.地址 https://pypi.python.org/pypi/dlib/18.17.100

  9. C#远程执行Linux系统中Shell命令和SFTP上传文件

    一.工具:SSH.Net 网址:https://github.com/sshnet/SSH.NET 二.调用命令代码: Renci.SshNet.SshClient ssh = "); ss ...

  10. NGINX源码分析——概览

    一.概况 Nginx可以开启多个进程,每个进程拥有最大上限128个子线程以及一定的可用连接数.最大客户端连接数等于进程数与连接数的乘积,连接是在主进程中初始化的,一开始所有连接处于空闲状态.每一个客户 ...