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()
Java交替打印两个字符串的更多相关文章
- java string截取两个字符串之间的值
java string截取两个字符串之间的值 import java.util.regex.Matcher; import java.util.regex.Pattern; public class ...
- Java 中如何计算两个字符串时间之间的时间差?(单位为分钟)
Java 中如何计算两个字符串时间之间的时间差?(单位为分钟) import java.text.DateFormat; import java.text.ParseException; import ...
- Java-Runoob-高级教程-实例-字符串:10. Java 实例 - 测试两个字符串区域是否相等-uncheck
ylbtech-Java-Runoob-高级教程-实例-字符串:10. Java 实例 - 测试两个字符串区域是否相等 1.返回顶部 1. Java 实例 - 测试两个字符串区域是否相等 Java ...
- java 算法之 两个字符串中最大相同的子串
public class String_intern { public static void main(String[] args) { String old="aaaaabc1" ...
- java中判断两个字符串是否相等的问题
我最近刚学java,今天编程的时候就遇到一个棘手的问题,就是关于判断两个字符串是否相等的问题.在编程中,通常比较两个字符串是否相同的表达式是“==”,但在java中不能这么写.在java中,用的是eq ...
- java操作对比两个字符串,将差异数据提取出来
记录瞬间 在实际的工作中,需要解决生成两次字符串结果进行对比的问题,将存在差异的字符串直接给出来. 当然,前提是需要将对比的两次结果,进行前期处理 比如: a_str = "@com/ene ...
- 用Java编程找到两个字符串中共有的字符
这道题的算法思想是把字符串1中的每个字符与字符串2中的每个字符进行比较,遇到共同拥有的字符,放入另一个数组中,最后顺序输出即可 但是这道题的难点在于怎么排除重复的字符 public class bot ...
- java例题_46 两个字符串拼接问题!
1 /*46 [程序 46 字符串连接] 2 题目:两个字符串连接程序,将两个字符串拼接在一起 3 */ 4 5 /*分析 6 * 两个字符串的拼接方法 7 * concat方式 8 * 当两个量都为 ...
- 【Java】获取两个字符串中最大相同子串
题目 获取两个字符串中最大相同子串 前提 两个字符串中只有一个最大相同子串 解决方案 public class StringDemo { public static void main(String[ ...
随机推荐
- centos7 开机自动连网
新安装好的CentOS7桌面版,默认的网络都是关闭的,在图形页面中,开启后,重启后网络又关闭了.下面配置开机自动连网. 工具/原料 CentOS7.2 方法/步骤 首先打开终端,用ifco ...
- 网页重构应该避免的10大 CSS 糟糕用法
对于网页重构来说,CSS禅意花园 是网页布局从 table 表格转到了 html +css 的标志 .这些年来,随着我们的网站越来越复杂:html5,css3,新的技术.新的属性,越来越多的开发者开始 ...
- 大数据开发实战:Hive优化实战3-大表join大表优化
5.大表join大表优化 如果Hive优化实战2中mapjoin中小表dim_seller很大呢?比如超过了1GB大小?这种就是大表join大表的问题.首先引入一个具体的问题场景,然后基于此介绍各自优 ...
- centos下配置sftp且限制用户访问目录
SFTP在Linux下是一个很方便很安全的文件传输工具,我常常用它在Linux服务器上替代传统的ftp来传输文件.众所周知SFTP账号是基于SSH账号的,默认情况下访问服务器的权限很大,下面的教程就是 ...
- 【R】函数-其它实用函数
- GIT 如何从另一分支合并特定的文件
是否遇到过这种情景: 您在一个分支上工作,发现该分支上的某些文件实现的功能已经在其他分支上实现了 但因为这两个分支实现不同的功能,因此不能进行简单的合并工作,但您又不想重复其他已经完成的工作 以下操作 ...
- THINKPHP 解决模块不存在时出现空页面的问题
遇到的问题: 最近使用THINKCMF开发了一个企业网站,因为之前客户的域名变更过,然后就发现当某个模块不存在的时候就出现了空页面 在 THINKPHP论坛 上有人说在项目里添加一个EmptyActi ...
- Docker: Unknown – Unable to query docker version: x509: certificate is valid for
I was playing around with Docker locally and somehow ended up with this error when I tried to list ...
- External component has thrown an exception
C#调用c++的DLL报错:External component has thrown an exception, 也没有log产生,怎么回事那? [解决方法] 这是因为c++的程序报错了,而且没有c ...
- 在webstorm中配置sass环境
最近开始用SASS,LESS等来写CSS,而在Webstorm中,它自带一个File Watchers功能,设置一下,即可实时编译SASS,LESS等. LESS的实时编译很简单,在node平台安装一 ...