Java每日一练02
- 数组元素统计
- 多线程按指定顺序执行
- 题目描述
- Semaphore信号量实现
- synchronized同步锁实现
- volatile实现
- CountDownLatch倒计时门闩实现
数组元素统计
class RandomTest {
public static void main(String[] args) {
// 输出arr1数组
int[] arr1 = storingRandomNumbers(5, 1, 9);
System.out.println(Arrays.toString(arr1));
int[] ints = numberModuloStatistics(arr1, 1, 3);
System.out.println(Arrays.toString(ints));
}
private static int[] storingRandomNumbers(int quantity, int start, int end) {
int arr1[] = new int[quantity];
for (int i = start - 1; i < arr1.length; i++) {
arr1[i] = new Random().nextInt(end) + 1;
}
return arr1;
}
private static int[] numberModuloStatistics(int[] arr1,int modnum1,int modnum2) {
int i2 = 0,j3 = 0;
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] % modnum1 == 0) {
i2++;
}
if (arr1[i] % modnum2 == 0) {
j3++;
}
}
int arr2[] = new int[2];
arr2[0] = i2;
arr2[1] = j3;
return arr2;
}
}
多线程按指定顺序执行
题目描述
import java.util.concurrent.Semaphore;
class Demo {
public static void main(String[] args) {
AnotherDummy ad = new AnotherDummy();
Thread t1 = new Thread(() -> {
try {
ad.first(() -> {
// business logic code
System.out.println("first");
});
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread t2 = new Thread(() -> {
try {
ad.second(() -> {
// business logic code
System.out.println("second");
});
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread t3 = new Thread(() -> {
try {
ad.third(() -> {
// business logic code
System.out.println("third");
});
} catch (InterruptedException e) {
e.printStackTrace();
}
});
t2.start();
t1.start();
t3.start();
}
}
Semaphore信号量实现
class AnotherDummy {
// The count of permits is 0, the threads will be blocked.
// Until, the resource can be acquired.
private Semaphore first = new Semaphore(0);
private Semaphore second = new Semaphore(0);
private Semaphore third = new Semaphore(0);
public void first(Runnable runnable) throws InterruptedException {
runnable.run();
first.release(); // release方法会释放持有许可证的线程,并归还Semaphore一个可用的许可证
second.release();
}
public void second(Runnable runnable) throws InterruptedException {
second.acquire(); // 调用acquire方法会阻塞线程执行,直到获取下一个许可证
runnable.run();
second.release();
third.release();
}
public void third(Runnable runnable) throws InterruptedException {
third.acquire();
runnable.run();
third.release();
}
}
synchronized同步锁实现
class Foo {
private boolean firstFinished;
private boolean secondFinished;
private Object lock = new Object();
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
synchronized (lock) {
printFirst.run();
firstFinished = true;
lock.notifyAll(); // 唤醒其它线程
}
}
public void second(Runnable printSecond) throws InterruptedException {
synchronized (lock) {
while (!firstFinished) {
lock.wait();
}
printSecond.run();
secondFinished = true;
lock.notifyAll(); // 唤醒其它线程
}
}
public void third(Runnable printThird) throws InterruptedException {
synchronized (lock) {
while (!secondFinished) {
lock.wait();
}
printThird.run();
}
}
}
volatile实现
class Foo2 {
public Foo2() {
}
volatile int count=1;
public void first(Runnable printFirst) throws InterruptedException {
printFirst.run();
count++;
}
public void second(Runnable printSecond) throws InterruptedException {
while (count!=2);
printSecond.run();
count++;
}
public void third(Runnable printThird) throws InterruptedException {
while (count!=3);
printThird.run();
}
}
CountDownLatch倒计时门闩实现
class Foo3 {
private CountDownLatch second = new CountDownLatch(1);
private CountDownLatch third = new CountDownLatch(1);
public Foo3() {
}
public void first(Runnable printFirst) throws InterruptedException {
printFirst.run();
second.countDown();
}
public void second(Runnable printSecond) throws InterruptedException {
second.await();
printSecond.run();
third.countDown();
}
public void third(Runnable printThird) throws InterruptedException {
third.await();
printThird.run();
}
}