Java 11 migraiton: mutex
This commit is contained in:
parent
2fa938c02d
commit
d733122e7a
@ -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();
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
@ -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;
|
||||||
|
@ -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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user