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.
*/
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();
}

View File

@ -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;

View File

@ -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;

View File

@ -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[]{});
}
}

View File

@ -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());
}
}
}

View File

@ -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();