JAVA-12045 : move java concurrency ebook content code to common module (#12559)
* JAVA-12045 : move java concurrency ebook content code to common module * JAVA-12045: addressed the review comments .. renamed package names and placed core-java-concurrency-simple module in pom.xml * JAVA-12045: removed duplicate module after renaming java-simple-module to core-java-simple-module
This commit is contained in:
@@ -12,5 +12,4 @@ This module contains articles about advanced topics about multithreading with co
|
||||
- [Guide to the Java Phaser](https://www.baeldung.com/java-phaser)
|
||||
- [An Introduction to Atomic Variables in Java](https://www.baeldung.com/java-atomic-variables)
|
||||
- [CyclicBarrier in Java](https://www.baeldung.com/java-cyclic-barrier)
|
||||
- [Guide to the Volatile Keyword in Java](https://www.baeldung.com/java-volatile)
|
||||
- More Articles: [[next -->]](/core-java-modules/core-java-concurrency-advanced-2)
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.baeldung.concurrent.daemon;
|
||||
|
||||
public class MultipleThreadsExample {
|
||||
public static void main(String[] args) {
|
||||
NewThread t1 = new NewThread();
|
||||
t1.setName("MyThread-1");
|
||||
NewThread t2 = new NewThread();
|
||||
t2.setName("MyThread-2");
|
||||
t1.start();
|
||||
t2.start();
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.baeldung.concurrent.daemon;
|
||||
|
||||
public class NewThread extends Thread {
|
||||
public void run() {
|
||||
long startTime = System.currentTimeMillis();
|
||||
while (true) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
System.out.println(this.getName() + ": New Thread is running..." + i);
|
||||
try {
|
||||
//Wait for one sec so it doesn't print too fast
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
// prevent the Thread to run forever. It will finish it's execution after 2 seconds
|
||||
if (System.currentTimeMillis() - startTime > 2000) {
|
||||
Thread.currentThread().interrupt();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.concurrent.daemon;
|
||||
|
||||
public class SingleThreadExample {
|
||||
public static void main(String[] args) {
|
||||
NewThread t = new NewThread();
|
||||
t.start();
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.concurrent.volatilekeyword;
|
||||
|
||||
|
||||
public class SharedObject {
|
||||
private volatile int count=0;
|
||||
|
||||
void incrementCount(){
|
||||
count++;
|
||||
}
|
||||
public int getCount(){
|
||||
return count;
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.concurrent.volatilekeyword;
|
||||
|
||||
public class TaskRunner {
|
||||
|
||||
private static int number;
|
||||
private volatile static boolean ready;
|
||||
|
||||
private static class Reader extends Thread {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (!ready) {
|
||||
Thread.yield();
|
||||
}
|
||||
|
||||
System.out.println(number);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Reader().start();
|
||||
number = 42;
|
||||
ready = true;
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package com.baeldung.concurrent.volatilekeyword;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class SharedObjectManualTest {
|
||||
|
||||
@Test
|
||||
public void whenOneThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
|
||||
SharedObject sharedObject = new SharedObject();
|
||||
|
||||
Thread writer = new Thread(() -> sharedObject.incrementCount());
|
||||
writer.start();
|
||||
Thread.sleep(100);
|
||||
|
||||
Thread readerOne = new Thread(() -> {
|
||||
int valueReadByThread2 = sharedObject.getCount();
|
||||
assertEquals(1, valueReadByThread2);
|
||||
});
|
||||
readerOne.start();
|
||||
|
||||
Thread readerTwo = new Thread(() -> {
|
||||
int valueReadByThread3 = sharedObject.getCount();
|
||||
assertEquals(1, valueReadByThread3);
|
||||
});
|
||||
readerTwo.start();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTwoThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
|
||||
SharedObject sharedObject = new SharedObject();
|
||||
Thread writerOne = new Thread(() -> sharedObject.incrementCount());
|
||||
writerOne.start();
|
||||
Thread.sleep(100);
|
||||
|
||||
Thread writerTwo = new Thread(() -> sharedObject.incrementCount());
|
||||
writerTwo.start();
|
||||
Thread.sleep(100);
|
||||
|
||||
Thread readerOne = new Thread(() -> {
|
||||
int valueReadByThread2 = sharedObject.getCount();
|
||||
assertEquals(2, valueReadByThread2);
|
||||
});
|
||||
readerOne.start();
|
||||
|
||||
Thread readerTwo = new Thread(() -> {
|
||||
int valueReadByThread3 = sharedObject.getCount();
|
||||
assertEquals(2, valueReadByThread3);
|
||||
});
|
||||
readerTwo.start();
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user