Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2b70fcf

Browse files
author
Maciej Mozolewski
committed
- blockin queue
1 parent 50ae761 commit 2b70fcf

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

JavaMultiThreadingCodes/src/CountDownLatch_6/App.java renamed to JavaMultiThreadingCodes/src/CountDownLatch_6/AppCountDownLatch.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,18 @@ public void run() {
4848
System.out.println("Started.");
4949

5050
try {
51-
Thread.sleep(3000);
51+
Thread.sleep(300);
5252
} catch (InterruptedException ignored) {}
5353
latch.countDown();
5454
}
5555
}
5656

57-
public class App {
57+
public class AppCountDownLatch {
5858

5959
public static void main(String[] args) {
60-
CountDownLatch latch = new CountDownLatch(3);
61-
ExecutorService executor = Executors.newFixedThreadPool(3);
62-
for (int i = 0; i < 3; i++) {
60+
CountDownLatch latch = new CountDownLatch(5);
61+
ExecutorService executor = Executors.newFixedThreadPool(2);
62+
for (int i = 0; i < 10; i++) {
6363
executor.submit(new Processor(latch));
6464
}
6565
executor.shutdown();

JavaMultiThreadingCodes/src/ProducerConsumer_7/App.java renamed to JavaMultiThreadingCodes/src/ProducerConsumer_7/AppProducerConsumer.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@
1515
* <br>
1616
* also freely available at
1717
* <a href="https://www.udemy.com/java-multithreading/?couponCode=FREE">
18-
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em>
18+
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em>
1919
* </a>
2020
*
2121
* @author Z.B. Celik <[email protected]>
2222
*/
23+
2324
import java.util.Random;
2425
import java.util.concurrent.ArrayBlockingQueue;
2526
import java.util.concurrent.BlockingQueue;
2627

2728
@SuppressWarnings("InfiniteLoopStatement")
28-
public class App {
29+
public class AppProducerConsumer {
2930

3031
/**
3132
* Thread safe implementation of {@link java.util.Queue} data structure so
@@ -48,15 +49,17 @@ public static void main(String[] args) throws InterruptedException {
4849
public void run() {
4950
try {
5051
producer();
51-
} catch (InterruptedException ignored) {}
52+
} catch (InterruptedException ignored) {
53+
}
5254
}
5355
});
5456

5557
Thread t2 = new Thread(new Runnable() {
5658
public void run() {
5759
try {
5860
consumer();
59-
} catch (InterruptedException ignored) {}
61+
} catch (InterruptedException ignored) {
62+
}
6063
}
6164
});
6265
t1.start();
@@ -73,18 +76,17 @@ public void run() {
7376
private static void producer() throws InterruptedException {
7477
Random random = new Random();
7578
while (true) {//loop indefinitely
79+
Thread.sleep(random.nextInt(210));
7680
queue.put(random.nextInt(100));//if queue is full (10) waits
7781
}
7882
}
7983

8084
private static void consumer() throws InterruptedException {
8185
Random random = new Random();
8286
while (true) {
83-
Thread.sleep(100);
84-
if (random.nextInt(10) == 0) {
87+
Thread.sleep(random.nextInt(200));
8588
Integer value = queue.take();//if queue is empty waits
8689
System.out.println("Taken value: " + value + "; Queue size is: " + queue.size());
8790
}
8891
}
8992
}
90-
}

JavaMultiThreadingCodes/src/ThreadPools_5/App.java renamed to JavaMultiThreadingCodes/src/ThreadPools_5/AppThreadPools.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
* <br>
1111
* also freely available at
1212
* <a href="https://www.udemy.com/java-multithreading/?couponCode=FREE">
13-
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em>
13+
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em>
1414
* </a>
1515
*
1616
* @author Z.B. Celik <[email protected]>
1717
*/
18+
1819
import java.util.concurrent.ExecutorService;
1920
import java.util.concurrent.Executors;
2021
import java.util.concurrent.TimeUnit;
@@ -37,21 +38,22 @@ public void run() {
3738
}
3839
}
3940

40-
public class App {
41+
public class AppThreadPools {
4142

4243
public static void main(String[] args) {
4344
/**
4445
* Created 2 threads, and assign tasks (Processor(i).run) to the threads
4546
*/
4647
ExecutorService executor = Executors.newFixedThreadPool(2);//2 Threads
47-
for (int i = 0; i < 2; i++) { // call the (Processor(i).run) 2 times with 2 threads
48+
for (int i = 0; i < 5; i++) { // call the (Processor(i).run) 2 times with 2 threads
4849
executor.submit(new Processor(i));
4950
}
5051
executor.shutdown();
5152
System.out.println("All tasks submitted.");
5253
try {
53-
executor.awaitTermination(1, TimeUnit.DAYS);
54+
executor.awaitTermination(1, TimeUnit.MINUTES);
5455
} catch (InterruptedException ignored) {
56+
throw new RuntimeException(ignored);
5557
}
5658
System.out.println("All tasks completed.");
5759
}

0 commit comments

Comments
 (0)