-
Notifications
You must be signed in to change notification settings - Fork 1
/
VolatileTest.java
48 lines (40 loc) · 1.57 KB
/
VolatileTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import org.junit.Assert;
import org.junit.Test;
import java.util.concurrent.atomic.AtomicLong;
/**
* Сравнение обычного счётчика, volatile и atomic
*/
public class VolatileTest extends Assert {
long counter = 0;
volatile long volatileCounter = 0;
AtomicLong atomicLong = new AtomicLong(0);
@Test
public void testVolatile() throws InterruptedException {
long expectedValue = 0;
for (int t = 0; t < 2; t++) {
// Создаём много потоков
int numberOfThreads = 1000;
int numberOfIterations = 1000;
expectedValue += numberOfThreads * numberOfIterations;
Thread th[] = new Thread[numberOfThreads];
for (int i = 0; i < numberOfThreads; ++i) {
th[i] = new Thread(() -> {
for (int j = 0; j < numberOfIterations; j++) {
counter++;
volatileCounter++;
atomicLong.incrementAndGet();
}
});
th[i].start();
}
// Ожидаем пока все потоки завершатся
for (Thread thread : th) thread.join();
assertEquals(expectedValue, atomicLong.intValue());
assertTrue(counter < expectedValue);
assertTrue(volatileCounter < expectedValue);
System.out.println("counter = " + counter +
" volatile = " + volatileCounter +
" atomic = " + atomicLong);
}
}
}