两个线程按顺序打印1~100的几种实现方法
1.atomicIntegerImpl
2.volatileLockImpl
3.reentrantLockImpl
4.synchronizedImpl
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
class Solution {
// synchronizedImpl使用的变量
static Object lock = new Object();
static int currentValue = 1;
// Lock实现所需要的变量
static ReentrantLock reentrantLock = new ReentrantLock();
// volatile实现所需要的变量,volatile变量的单一操作,可以实现局部的同步效果
// 但是volatile无法保证原子性,所以如果有i++这样的操作,volatile将会有问题
// 也就是说,这里循环打印1/2,没有问题,但是如果题目是循环打印1~100的整数,就不可以单单用volatile了
static volatile int currentXValue = 1;
// AtomicInteger实现
static AtomicInteger nAtomicInteger = new AtomicInteger(1);
public static void main(String[] args) {
// synchronizedImpl();
// reentrantLockImpl();
// volatileLockImpl();
atomicIntegerImpl();
}
/**
*
* @Title: atomicIntegerImpl
* @Description:AtomicInteger实现
* @author: itbird
* @date 2022年3月16日 下午3:54:39 void
*/
private static void atomicIntegerImpl() {
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
if (nAtomicInteger.intValue() % 2 == 0) {
System.out.println(Thread.currentThread().getName()
+ " " + nAtomicInteger.intValue());
nAtomicInteger.incrementAndGet();
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
if (nAtomicInteger.intValue() % 2 != 0) {
System.out.println(Thread.currentThread().getName()
+ " " + nAtomicInteger.intValue());
nAtomicInteger.incrementAndGet();
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
thread1.start();
thread2.start();
}
/**
*
* @Title: volatileLockImpl
* @Description: volatile实现
* @author: itbird
* @date 2022年3月16日 下午3:43:47 void
*/
private static void volatileLockImpl() {
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
if (currentXValue % 2 == 0) {
System.out.println(Thread.currentThread().getName()
+ " " + currentXValue);
currentXValue++;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
if (currentXValue % 2 != 0) {
System.out.println(Thread.currentThread().getName()
+ " " + currentXValue);
currentXValue++;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
thread1.start();
thread2.start();
}
/**
*
* @Title: reentrantLockImpl
* @Description: ReentrantLock实现方式
* @author: itbird
* @date 2022年3月16日 下午3:40:30 void
*/
private static void reentrantLockImpl() {
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
reentrantLock.lock();
if (currentValue % 2 != 0) {
System.out.println(Thread.currentThread().getName()
+ " " + currentValue);
currentValue++;
}
} finally {
reentrantLock.unlock();
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
reentrantLock.lock();
if (currentValue % 2 == 0) {
System.out.println(Thread.currentThread().getName()
+ " " + currentValue);
currentValue++;
}
} finally {
reentrantLock.unlock();
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
thread1.start();
thread2.start();
}
/**
* @Title: synchronizedImpl
* @Description: synchronized实现方式
* @author: itbird
* @date 2022年3月16日 下午3:14:43 void
*/
public static void synchronizedImpl() {
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
synchronized (lock) {
if (currentValue % 2 != 0) {
System.out.println(Thread.currentThread().getName()
+ " " + currentValue);
currentValue++;
lock.notify();
} else {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
synchronized (lock) {
if (currentValue % 2 == 0) {
System.out.println(Thread.currentThread().getName()
+ " " + currentValue);
currentValue++;
lock.notify();
} else {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
});
thread1.start();
thread2.start();
}
}