实现两个线程交替打印奇偶数
1. 同步代码块
/**
* 交替打印奇数偶数
*
* @author cxyxh
* @date 2021-08-06
*/
public class AlternatePrintSynchronized {
//即将要打印的数
static Integer count = 0;
//需要打印到的最大值
static volatile Integer maxCount = 100;
public static void main(String[] args) {
//只有一个线程能进入到打印的代码块中,只需要各个线程确定当前数是否是自己能打印的数就行
Thread threadEven = new Thread(() -> {
while (count < maxCount){
synchronized (AlternatePrintSynchronized.class) {
//如果是奇数
if ( (count & 1) == 1){
System.out.println