这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.baeldung.concurrent.busywaiting;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.concurrent.atomic.AtomicBoolean;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BusyWaitingManualTest {

private static final Logger logger = LoggerFactory.getLogger(BusyWaitingManualTest.class);

@Test
void givenWorkerThread_whenBusyWaiting_thenAssertExecutedMultipleTimes() {
AtomicBoolean taskDone = new AtomicBoolean(false);
long counter = 0;

Thread worker = new Thread(() -> {
simulateThreadWork();
taskDone.set(true);
});

worker.start();

while (!taskDone.get()) {
counter++;
}

logger.info("Counter: {}", counter);
assertNotEquals(1, counter);
}

@Test
void givenWorkerThread_whenUsingWaitNotify_thenWaitEfficientlyOnce() {
AtomicBoolean taskDone = new AtomicBoolean(false);
final Object monitor = new Object();
long counter = 0;

Thread worker = new Thread(() -> {
simulateThreadWork();
synchronized (monitor) {
taskDone.set(true);
monitor.notify();
}
});

worker.start();

synchronized (monitor) {
while (!taskDone.get()) {
counter++;
try {
monitor.wait();
} catch (InterruptedException e) {
Thread.currentThread()
.interrupt();
fail("Test case failed due to thread interruption!");
}
}
}

assertEquals(1, counter);
}

private void simulateThreadWork() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Thread.currentThread()
.interrupt();
}
}

}