Skip to content

Commit 0416314

Browse files
author
Dassi Orleando
committed
Fix conflicts
2 parents 1fc3999 + 4f6988b commit 0416314

82 files changed

Lines changed: 1738 additions & 185 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎core-java-9/src/test/java/com/baeldung/java9/httpclient/SimpleHttpRequestsUnitTest.java‎

Lines changed: 0 additions & 126 deletions
This file was deleted.

‎core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public JobPriority getJobPriority() {
1616
@Override
1717
public void run() {
1818
try {
19-
System.out.println("Job:" + jobName + " Priority:" + jobPriority);
19+
System.out.println("Job:" + jobName +
20+
" Priority:" + jobPriority);
2021
Thread.sleep(1000);
2122
} catch (InterruptedException ignored) {
2223
}

‎core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobScheduler.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@
99
public class PriorityJobScheduler {
1010

1111
private ExecutorService priorityJobPoolExecutor;
12-
private ExecutorService priorityJobScheduler;
13-
private PriorityBlockingQueue<Runnable> priorityQueue;
12+
private ExecutorService priorityJobScheduler =
13+
Executors.newSingleThreadExecutor();
14+
private PriorityBlockingQueue<Job> priorityQueue;
1415

1516
public PriorityJobScheduler(Integer poolSize, Integer queueSize) {
1617
priorityJobPoolExecutor = Executors.newFixedThreadPool(poolSize);
17-
Comparator<? super Job> jobComparator = Comparator.comparing(Job::getJobPriority);
18-
priorityQueue = new PriorityBlockingQueue<Runnable>(queueSize,
19-
(Comparator<? super Runnable>) jobComparator);
18+
priorityQueue = new PriorityBlockingQueue<Job>(queueSize,
19+
Comparator.comparing(Job::getJobPriority));
2020

21-
priorityJobScheduler = Executors.newSingleThreadExecutor();
2221
priorityJobScheduler.execute(()->{
2322
while (true) {
2423
try {
2524
priorityJobPoolExecutor.execute(priorityQueue.take());
2625
} catch (InterruptedException e) {
26+
// exception needs special handling
2727
break;
2828
}
2929
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.concurrent.threadlifecycle;
2+
3+
public class BlockedState {
4+
public static void main(String[] args) throws InterruptedException {
5+
Thread t1 = new Thread(new DemoThreadB());
6+
Thread t2 = new Thread(new DemoThreadB());
7+
8+
t1.start();
9+
t2.start();
10+
11+
Thread.sleep(1000);
12+
13+
System.out.println(t2.getState());
14+
System.exit(0);
15+
}
16+
}
17+
18+
class DemoThreadB implements Runnable {
19+
@Override
20+
public void run() {
21+
commonResource();
22+
}
23+
24+
public static synchronized void commonResource() {
25+
while(true) {
26+
// Infinite loop to mimic heavy processing
27+
// Thread 't1' won't leave this method
28+
// when Thread 't2' enters this
29+
}
30+
}
31+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.concurrent.threadlifecycle;
2+
3+
public class NewState implements Runnable {
4+
public static void main(String[] args) {
5+
Runnable runnable = new NewState();
6+
Thread t = new Thread(runnable);
7+
System.out.println(t.getState());
8+
}
9+
10+
@Override
11+
public void run() {
12+
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.concurrent.threadlifecycle;
2+
3+
public class RunnableState implements Runnable {
4+
public static void main(String[] args) {
5+
Runnable runnable = new NewState();
6+
Thread t = new Thread(runnable);
7+
t.start();
8+
System.out.println(t.getState());
9+
}
10+
11+
@Override
12+
public void run() {
13+
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.concurrent.threadlifecycle;
2+
3+
public class TerminatedState implements Runnable {
4+
public static void main(String[] args) throws InterruptedException {
5+
Thread t1 = new Thread(new TerminatedState());
6+
t1.start();
7+
Thread.sleep(1000);
8+
System.out.println(t1.getState());
9+
}
10+
11+
@Override
12+
public void run() {
13+
// No processing in this block
14+
}
15+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.concurrent.threadlifecycle;
2+
3+
public class TimedWaitingState {
4+
public static void main(String[] args) throws InterruptedException {
5+
DemoThread obj1 = new DemoThread();
6+
Thread t1 = new Thread(obj1);
7+
t1.start();
8+
// The following sleep will give enough time for ThreadScheduler
9+
// to start processing of thread t1
10+
Thread.sleep(1000);
11+
System.out.println(t1.getState());
12+
}
13+
}
14+
15+
class DemoThread implements Runnable {
16+
@Override
17+
public void run() {
18+
try {
19+
Thread.sleep(5000);
20+
} catch (InterruptedException e) {
21+
Thread.currentThread().interrupt();
22+
e.printStackTrace();
23+
}
24+
}
25+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.concurrent.threadlifecycle;
2+
3+
public class WaitingState implements Runnable {
4+
public static Thread t1;
5+
6+
public static void main(String[] args) {
7+
t1 = new Thread(new WaitingState());
8+
t1.start();
9+
}
10+
11+
public void run() {
12+
Thread t2 = new Thread(new DemoThreadWS());
13+
t2.start();
14+
15+
try {
16+
t2.join();
17+
} catch (InterruptedException e) {
18+
Thread.currentThread().interrupt();
19+
e.printStackTrace();
20+
}
21+
}
22+
}
23+
24+
class DemoThreadWS implements Runnable {
25+
public void run() {
26+
try {
27+
Thread.sleep(1000);
28+
} catch (InterruptedException e) {
29+
Thread.currentThread().interrupt();
30+
e.printStackTrace();
31+
}
32+
33+
System.out.println(WaitingState.t1.getState());
34+
}
35+
}

‎core-java-concurrency/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public void whenMultiplePriorityJobsQueued_thenHighestPriorityJobIsPicked() {
3030
// delay to avoid job sleep (added for demo) being interrupted
3131
try {
3232
Thread.sleep(2000);
33-
} catch (InterruptedException ignored) {
33+
} catch (InterruptedException e) {
34+
Thread.currentThread().interrupt();
35+
throw new RuntimeException(e);
3436
}
3537

3638
pjs.closeScheduler();

0 commit comments

Comments
 (0)