Skip to content

Commit bb38c5e

Browse files
String Alignment Examples
1 parent 9ce2a42 commit bb38c5e

File tree

6 files changed

+505
-70
lines changed

6 files changed

+505
-70
lines changed

pom.xml

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,17 @@
177177
<artifactId>jakarta.mail</artifactId>
178178
<version>2.0.1</version>
179179
</dependency>
180+
<dependency>
181+
<groupId>org.jetbrains</groupId>
182+
<artifactId>annotations</artifactId>
183+
<version>17.0.0</version>
184+
<scope>compile</scope>
185+
</dependency>
186+
<dependency>
187+
<groupId>org.apache.commons</groupId>
188+
<artifactId>commons-text</artifactId>
189+
<version>1.10.0</version>
190+
</dependency>
180191
</dependencies>
181192
<build>
182193
<sourceDirectory>src/main/java</sourceDirectory>
@@ -188,33 +199,33 @@
188199
</excludes>
189200
</resource>
190201
</resources>
191-
<plugins>
192-
<plugin>
193-
<groupId>org.apache.maven.plugins</groupId>
194-
<artifactId>maven-compiler-plugin</artifactId>
195-
<version>3.8.1</version>
196-
<configuration>
197-
<source>17</source>
198-
<target>17</target>
199-
<annotationProcessorPaths>
200-
<path>
201-
<groupId>org.projectlombok</groupId>
202-
<artifactId>lombok</artifactId>
203-
<version>1.18.22</version>
204-
</path>
205-
<path>
206-
<groupId>io.soabase.record-builder</groupId>
207-
<artifactId>record-builder-processor</artifactId>
208-
<version>34</version>
209-
</path>
210-
<path>
211-
<groupId>org.openjdk.jmh</groupId>
212-
<artifactId>jmh-generator-annprocess</artifactId>
213-
<version>${jmh.version}</version>
214-
</path>
215-
</annotationProcessorPaths>
216-
</configuration>
217-
</plugin>
202+
<plugins>
203+
<plugin>
204+
<groupId>org.apache.maven.plugins</groupId>
205+
<artifactId>maven-compiler-plugin</artifactId>
206+
<version>3.8.1</version>
207+
<configuration>
208+
<source>17</source>
209+
<target>17</target>
210+
<annotationProcessorPaths>
211+
<path>
212+
<groupId>org.projectlombok</groupId>
213+
<artifactId>lombok</artifactId>
214+
<version>1.18.22</version>
215+
</path>
216+
<path>
217+
<groupId>io.soabase.record-builder</groupId>
218+
<artifactId>record-builder-processor</artifactId>
219+
<version>34</version>
220+
</path>
221+
<path>
222+
<groupId>org.openjdk.jmh</groupId>
223+
<artifactId>jmh-generator-annprocess</artifactId>
224+
<version>${jmh.version}</version>
225+
</path>
226+
</annotationProcessorPaths>
227+
</configuration>
228+
</plugin>
218229
<plugin>
219230
<groupId>org.apache.maven.plugins</groupId>
220231
<artifactId>maven-shade-plugin</artifactId>

src/main/java/com/howtodoinjava/cleaner/CleanerExample.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package com.howtodoinjava.cleaner;
22

33
public class CleanerExample {
4+
45
public static void main(final String[] args) throws Exception {
5-
6+
67
//1 Implicit Cleanup
7-
try (final ClassAccessingResource clazzInstance
8-
= new ClassAccessingResource()) {
8+
try (final ClassAccessingResource clazzInstance
9+
= new ClassAccessingResource()) {
910
// Safely use the resource
1011
clazzInstance.businessOperation();
1112
clazzInstance.anotherBusinessOperation();
1213
}
13-
14+
1415
//2 Explicit Cleanup
1516
final ClassAccessingResource clazzInstance = new ClassAccessingResource();
1617
clazzInstance.businessOperation();

src/main/java/com/howtodoinjava/concurrency/BlockingThreadPoolExecutor.java

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,50 @@
33
import java.util.concurrent.*;
44

55
public class BlockingThreadPoolExecutor extends ThreadPoolExecutor {
6-
private final Semaphore semaphore;
76

8-
public BlockingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
9-
TimeUnit unit, BlockingQueue<Runnable> workQueue) {
10-
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
11-
semaphore = new Semaphore(maximumPoolSize);
12-
}
7+
private final Semaphore semaphore;
138

14-
@Override
15-
protected void beforeExecute(Thread t, Runnable r) {
16-
super.beforeExecute(t, r);
17-
}
9+
public BlockingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
10+
TimeUnit unit, BlockingQueue<Runnable> workQueue) {
11+
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
12+
semaphore = new Semaphore(maximumPoolSize);
13+
}
1814

15+
@Override
16+
protected void beforeExecute(Thread t, Runnable r) {
17+
super.beforeExecute(t, r);
18+
}
1919

20-
@Override
21-
public void execute(final Runnable task) {
22-
23-
boolean acquired = false;
24-
25-
do {
26-
try {
27-
semaphore.acquire();
28-
acquired = true;
29-
} catch (final InterruptedException e) {
30-
e.printStackTrace();
31-
}
32-
} while (!acquired);
33-
34-
try {
35-
super.execute(task);
36-
} catch (final RejectedExecutionException e) {
37-
System.out.println("Task Rejected");
38-
semaphore.release();
39-
return;
40-
}
41-
semaphore.release();
42-
}
4320

44-
@Override
45-
protected void afterExecute(Runnable r, Throwable t) {
46-
super.afterExecute(r, t);
47-
if (t != null) {
48-
t.printStackTrace();
49-
}
21+
@Override
22+
public void execute(final Runnable task) {
23+
24+
boolean acquired = false;
25+
26+
do {
27+
try {
28+
semaphore.acquire();
29+
acquired = true;
30+
} catch (final InterruptedException e) {
31+
e.printStackTrace();
32+
}
33+
} while (!acquired);
34+
35+
try {
36+
super.execute(task);
37+
} catch (final RejectedExecutionException e) {
38+
System.out.println("Task Rejected");
39+
semaphore.release();
40+
return;
41+
}
42+
semaphore.release();
43+
}
44+
45+
@Override
46+
protected void afterExecute(Runnable r, Throwable t) {
47+
super.afterExecute(r, t);
48+
if (t != null) {
49+
t.printStackTrace();
5050
}
51+
}
5152
}

0 commit comments

Comments
 (0)