Java 11 migraiton: mutex
This commit is contained in:
parent
2fa938c02d
commit
d733122e7a
@ -38,10 +38,10 @@ public class App {
|
||||
* main method.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Mutex mutex = new Mutex();
|
||||
Jar jar = new Jar(1000, mutex);
|
||||
Thief peter = new Thief("Peter", jar);
|
||||
Thief john = new Thief("John", jar);
|
||||
var mutex = new Mutex();
|
||||
var jar = new Jar(1000, mutex);
|
||||
var peter = new Thief("Peter", jar);
|
||||
var john = new Thief("John", jar);
|
||||
peter.start();
|
||||
john.start();
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ public class Jar {
|
||||
* Method for a thief to take a bean.
|
||||
*/
|
||||
public boolean takeBean() {
|
||||
boolean success = false;
|
||||
var success = false;
|
||||
try {
|
||||
lock.acquire();
|
||||
success = beans > 0;
|
||||
|
@ -54,7 +54,7 @@ public class Thief extends Thread {
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
int beans = 0;
|
||||
var beans = 0;
|
||||
|
||||
while (jar.takeBean()) {
|
||||
beans = beans + 1;
|
||||
|
@ -25,15 +25,12 @@ package com.iluwatar.mutex;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Application Test Entrypoint
|
||||
*/
|
||||
public class AppTest {
|
||||
@Test
|
||||
public void test() throws IOException {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
}
|
||||
}
|
||||
|
@ -23,10 +23,11 @@
|
||||
|
||||
package com.iluwatar.mutex;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Test case for taking beans from a Jar
|
||||
@ -35,12 +36,10 @@ public class JarTest {
|
||||
|
||||
@Test
|
||||
public void testTakeBeans() {
|
||||
Mutex mutex = new Mutex();
|
||||
Jar jar = new Jar(10, mutex);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
assertTrue(jar.takeBean());
|
||||
}
|
||||
var mutex = new Mutex();
|
||||
var jar = new Jar(10, mutex);
|
||||
IntStream.range(0, 10).mapToObj(i -> jar.takeBean()).forEach(Assertions::assertTrue);
|
||||
assertFalse(jar.takeBean());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -23,12 +23,12 @@
|
||||
|
||||
package com.iluwatar.mutex;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Test case for acquiring and releasing a Mutex
|
||||
*/
|
||||
@ -36,7 +36,7 @@ public class MutexTest {
|
||||
|
||||
@Test
|
||||
public void acquireReleaseTest() {
|
||||
Mutex mutex = new Mutex();
|
||||
var mutex = new Mutex();
|
||||
assertNull(mutex.getOwner());
|
||||
try {
|
||||
mutex.acquire();
|
||||
|
Loading…
x
Reference in New Issue
Block a user