Java 11 migraiton: mutex

This commit is contained in:
Anurag Agarwal 2020-04-12 23:00:49 +00:00
parent 2fa938c02d
commit d733122e7a
No known key found for this signature in database
GPG Key ID: CF5E14552DA23F13
6 changed files with 19 additions and 23 deletions

View File

@ -38,10 +38,10 @@ public class App {
* main method. * main method.
*/ */
public static void main(String[] args) { public static void main(String[] args) {
Mutex mutex = new Mutex(); var mutex = new Mutex();
Jar jar = new Jar(1000, mutex); var jar = new Jar(1000, mutex);
Thief peter = new Thief("Peter", jar); var peter = new Thief("Peter", jar);
Thief john = new Thief("John", jar); var john = new Thief("John", jar);
peter.start(); peter.start();
john.start(); john.start();
} }

View File

@ -48,7 +48,7 @@ public class Jar {
* Method for a thief to take a bean. * Method for a thief to take a bean.
*/ */
public boolean takeBean() { public boolean takeBean() {
boolean success = false; var success = false;
try { try {
lock.acquire(); lock.acquire();
success = beans > 0; success = beans > 0;

View File

@ -54,7 +54,7 @@ public class Thief extends Thread {
*/ */
@Override @Override
public void run() { public void run() {
int beans = 0; var beans = 0;
while (jar.takeBean()) { while (jar.takeBean()) {
beans = beans + 1; beans = beans + 1;

View File

@ -25,15 +25,12 @@ package com.iluwatar.mutex;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.IOException;
/** /**
* Application Test Entrypoint * Application Test Entrypoint
*/ */
public class AppTest { public class AppTest {
@Test @Test
public void test() throws IOException { public void test() {
String[] args = {}; App.main(new String[]{});
App.main(args);
} }
} }

View File

@ -23,10 +23,11 @@
package com.iluwatar.mutex; 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.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 * Test case for taking beans from a Jar
@ -35,11 +36,9 @@ public class JarTest {
@Test @Test
public void testTakeBeans() { public void testTakeBeans() {
Mutex mutex = new Mutex(); var mutex = new Mutex();
Jar jar = new Jar(10, mutex); var jar = new Jar(10, mutex);
for (int i = 0; i < 10; i++) { IntStream.range(0, 10).mapToObj(i -> jar.takeBean()).forEach(Assertions::assertTrue);
assertTrue(jar.takeBean());
}
assertFalse(jar.takeBean()); assertFalse(jar.takeBean());
} }

View File

@ -23,12 +23,12 @@
package com.iluwatar.mutex; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.Test;
/** /**
* Test case for acquiring and releasing a Mutex * Test case for acquiring and releasing a Mutex
*/ */
@ -36,7 +36,7 @@ public class MutexTest {
@Test @Test
public void acquireReleaseTest() { public void acquireReleaseTest() {
Mutex mutex = new Mutex(); var mutex = new Mutex();
assertNull(mutex.getOwner()); assertNull(mutex.getOwner());
try { try {
mutex.acquire(); mutex.acquire();