Skip to content

Commit ab329fd

Browse files
committed
feat(concurrency): 🎸 execute tasks, translated
Refers: #8
1 parent e0e1023 commit ab329fd

18 files changed

+240
-242
lines changed

book/06-concurrency/sections/03-execute-tasks.asc

Lines changed: 138 additions & 140 deletions
Large diffs are not rendered by default.

src/org/j6toj8/concurrency/executetasks/Schedule_SingleThread.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public static void main(String[] args) {
1111
// tag::code[]
1212
ScheduledExecutorService executor = null;
1313
try {
14-
executor = Executors.newSingleThreadScheduledExecutor(); // executor de agendamento com uma única thread
15-
System.out.println("Agora: " + LocalTime.now()); // imprime a hora atual
14+
executor = Executors.newSingleThreadScheduledExecutor(); // single thread scheduler
15+
System.out.println("Now: " + LocalTime.now()); // print the current time
1616

17-
executor.schedule(() -> System.out.println("Execução: " + LocalTime.now()), 3, TimeUnit.SECONDS);
17+
executor.schedule(() -> System.out.println("Execution: " + LocalTime.now()), 3, TimeUnit.SECONDS);
1818
} finally {
1919
if (executor != null) {
2020
executor.shutdown();

src/org/j6toj8/concurrency/executetasks/Schedule_SingleThreadCallable.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ public static void main(String[] args) {
1313
// tag::code[]
1414
ScheduledExecutorService executor = null;
1515
try {
16-
executor = Executors.newSingleThreadScheduledExecutor(); // executor de agendamento com uma única thread
17-
System.out.println("Antes do agendamento: " + LocalTime.now());
18-
ScheduledFuture<String> retorno = executor.schedule(() -> "Execução: " + LocalTime.now(), 3, TimeUnit.SECONDS);
19-
System.out.println("Depois do agendamento: " + LocalTime.now());
16+
executor = Executors.newSingleThreadScheduledExecutor(); // single thread scheduler
17+
System.out.println("Before Scheduling: " + LocalTime.now());
18+
ScheduledFuture<String> futureReturn = executor.schedule(() -> "Execution: " + LocalTime.now(), 3, TimeUnit.SECONDS);
19+
System.out.println("After Scheduling: " + LocalTime.now());
2020

21-
System.out.println(retorno.get()); // fica parado aqui esperando o retorno
22-
System.out.println("Depois da execução: " + LocalTime.now());
21+
System.out.println(futureReturn.get()); // stands here waiting for the return
22+
System.out.println("After execution: " + LocalTime.now());
2323
} catch (InterruptedException | ExecutionException e) {
24-
System.out.println("Erro ao fazer .get()");
24+
System.out.println("Error doing .get()");
2525
} finally {
2626
if (executor != null) {
2727
executor.shutdown();

src/org/j6toj8/concurrency/executetasks/Schedule_SingleThreadFixedDelay.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ public class Schedule_SingleThreadFixedDelay {
1111
public static void main(String[] args) {
1212
ScheduledExecutorService executor = null;
1313
try {
14-
executor = Executors.newSingleThreadScheduledExecutor(); // executor de agendamento com uma única thread
15-
System.out.println("Antes do agendamento: " + LocalTime.now()); // imprime a hora atual
16-
executor.scheduleWithFixedDelay(() -> System.out.println("Execução: " + LocalTime.now()), 3, 1, TimeUnit.SECONDS);
17-
System.out.println("Após do agendamento: " + LocalTime.now()); // imprime a hora atual
18-
sleep(); // aguarda um tempo para ser possível enxergar as execuções
19-
System.out.println("Após o sleep de 10 segundos: " + LocalTime.now()); // imprime a hora atual
14+
executor = Executors.newSingleThreadScheduledExecutor(); // single thread scheduler
15+
System.out.println("Before Scheduling: " + LocalTime.now()); // print the current time
16+
executor.scheduleWithFixedDelay(() -> System.out.println("Execution: " + LocalTime.now()), 3, 1, TimeUnit.SECONDS);
17+
System.out.println("After Scheduling: " + LocalTime.now()); // print the current time
18+
sleep(); // waits a while to be able to see the executions
19+
System.out.println("After 10 seconds sleep: " + LocalTime.now()); // print the current time
2020
} finally {
2121
if (executor != null) {
22-
System.out.println("Invocando shutdown no executor.");
22+
System.out.println("Invoking shutdown on executor.");
2323
executor.shutdown();
2424
}
2525
}

src/org/j6toj8/concurrency/executetasks/Schedule_SingleThreadFixedRate.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ public class Schedule_SingleThreadFixedRate {
1111
public static void main(String[] args) {
1212
ScheduledExecutorService executor = null;
1313
try {
14-
executor = Executors.newSingleThreadScheduledExecutor(); // executor de agendamento com uma única thread
15-
System.out.println("Antes do agendamento: " + LocalTime.now()); // imprime a hora atual
16-
executor.scheduleAtFixedRate(() -> System.out.println("Execução: " + LocalTime.now()), 3, 1, TimeUnit.SECONDS);
17-
System.out.println("Após do agendamento: " + LocalTime.now()); // imprime a hora atual
18-
sleep(); // aguarda um tempo para ser possível enxergar as execuções
19-
System.out.println("Após o sleep de 10 segundos: " + LocalTime.now()); // imprime a hora atual
14+
executor = Executors.newSingleThreadScheduledExecutor(); // single thread scheduler
15+
System.out.println("Before Scheduling: " + LocalTime.now()); // print the current time
16+
executor.scheduleAtFixedRate(() -> System.out.println("Execution: " + LocalTime.now()), 3, 1, TimeUnit.SECONDS);
17+
System.out.println("After Scheduling: " + LocalTime.now()); // print the current time
18+
sleep(); // waits a while to be able to see the executions
19+
System.out.println("After 10 seconds sleep: " + LocalTime.now()); // print the current time
2020
} finally {
2121
if (executor != null) {
22-
System.out.println("Invocando shutdown no executor.");
22+
System.out.println("Invoking shutdown on executor.");
2323
executor.shutdown();
2424
}
2525
}

src/org/j6toj8/concurrency/executetasks/TasksMulti_CachedThreadPool.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public static void main(String[] args) {
99
// tag::code[]
1010
ExecutorService executor = null;
1111
try {
12-
executor = Executors.newCachedThreadPool(); // executor com cache de threads
13-
executor.execute(() -> System.out.println("Tarefa 1 - Thread do Executor: " + Thread.currentThread().getName()));
14-
executor.execute(() -> System.out.println("Tarefa 2 - Thread do Executor: " + Thread.currentThread().getName()));
15-
executor.execute(() -> System.out.println("Tarefa 3 - Thread do Executor: " + Thread.currentThread().getName()));
16-
executor.execute(() -> System.out.println("Tarefa 4 - Thread do Executor: " + Thread.currentThread().getName()));
17-
executor.execute(() -> System.out.println("Tarefa 5 - Thread do Executor: " + Thread.currentThread().getName()));
12+
executor = Executors.newCachedThreadPool(); // thread cached executor
13+
executor.execute(() -> System.out.println("Task 1 - Executor Thread: " + Thread.currentThread().getName()));
14+
executor.execute(() -> System.out.println("Task 2 - Executor Thread: " + Thread.currentThread().getName()));
15+
executor.execute(() -> System.out.println("Task 3 - Executor Thread: " + Thread.currentThread().getName()));
16+
executor.execute(() -> System.out.println("Task 4 - Executor Thread: " + Thread.currentThread().getName()));
17+
executor.execute(() -> System.out.println("Task 5 - Executor Thread: " + Thread.currentThread().getName()));
1818
} finally {
1919
if (executor != null) {
2020
executor.shutdown();

src/org/j6toj8/concurrency/executetasks/TasksMulti_FixedThreadPool.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public static void main(String[] args) {
99
// tag::code[]
1010
ExecutorService executor = null;
1111
try {
12-
executor = Executors.newFixedThreadPool(2); // executor com duas threads
13-
executor.execute(() -> System.out.println("Tarefa 1 - Thread do Executor: " + Thread.currentThread().getName()));
14-
executor.execute(() -> System.out.println("Tarefa 2 - Thread do Executor: " + Thread.currentThread().getName()));
15-
executor.execute(() -> System.out.println("Tarefa 3 - Thread do Executor: " + Thread.currentThread().getName()));
16-
executor.execute(() -> System.out.println("Tarefa 4 - Thread do Executor: " + Thread.currentThread().getName()));
17-
executor.execute(() -> System.out.println("Tarefa 5 - Thread do Executor: " + Thread.currentThread().getName()));
12+
executor = Executors.newFixedThreadPool(2); // two thread executor
13+
executor.execute(() -> System.out.println("Task 1 - Executor Thread: " + Thread.currentThread().getName()));
14+
executor.execute(() -> System.out.println("Task 2 - Executor Thread: " + Thread.currentThread().getName()));
15+
executor.execute(() -> System.out.println("Task 3 - Executor Thread: " + Thread.currentThread().getName()));
16+
executor.execute(() -> System.out.println("Task 4 - Executor Thread: " + Thread.currentThread().getName()));
17+
executor.execute(() -> System.out.println("Task 5 - Executor Thread: " + Thread.currentThread().getName()));
1818
} finally {
1919
if (executor != null) {
2020
executor.shutdown();

src/org/j6toj8/concurrency/executetasks/TasksMulti_ScheduledThreadPool.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ public static void main(String[] args) {
1111
// tag::code[]
1212
ScheduledExecutorService executor = null;
1313
try {
14-
executor = Executors.newScheduledThreadPool(2); // executor de agendamento com duas threads
15-
System.out.println("Agora: " + LocalTime.now()); // imprime a hora atual
14+
executor = Executors.newScheduledThreadPool(2); // two thread scheduling executor
15+
System.out.println("Now: " + LocalTime.now()); // print the current time
1616

17-
executor.schedule(() -> System.out.println("Execução 1: " + Thread.currentThread().getName() + " - " + LocalTime.now()), 3, TimeUnit.SECONDS);
18-
executor.schedule(() -> System.out.println("Execução 2: " + Thread.currentThread().getName() + " - " + LocalTime.now()), 3, TimeUnit.SECONDS);
19-
executor.schedule(() -> System.out.println("Execução 3: " + Thread.currentThread().getName() + " - " + LocalTime.now()), 3, TimeUnit.SECONDS);
20-
executor.schedule(() -> System.out.println("Execução 4: " + Thread.currentThread().getName() + " - " + LocalTime.now()), 3, TimeUnit.SECONDS);
21-
executor.schedule(() -> System.out.println("Execução 5: " + Thread.currentThread().getName() + " - " + LocalTime.now()), 3, TimeUnit.SECONDS);
17+
executor.schedule(() -> System.out.println("Execution 1: " + Thread.currentThread().getName() + " - " + LocalTime.now()), 3, TimeUnit.SECONDS);
18+
executor.schedule(() -> System.out.println("Execution 2: " + Thread.currentThread().getName() + " - " + LocalTime.now()), 3, TimeUnit.SECONDS);
19+
executor.schedule(() -> System.out.println("Execution 3: " + Thread.currentThread().getName() + " - " + LocalTime.now()), 3, TimeUnit.SECONDS);
20+
executor.schedule(() -> System.out.println("Execution 4: " + Thread.currentThread().getName() + " - " + LocalTime.now()), 3, TimeUnit.SECONDS);
21+
executor.schedule(() -> System.out.println("Execution 5: " + Thread.currentThread().getName() + " - " + LocalTime.now()), 3, TimeUnit.SECONDS);
2222
} finally {
2323
if (executor != null) {
2424
executor.shutdown();

src/org/j6toj8/concurrency/executetasks/Tasks_RunnableCallable.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ public static void main(String[] args) {
1111
try {
1212
executor = Executors.newSingleThreadExecutor();
1313

14-
// tarefa sem retorno, instância de Runnable
14+
// task without return, instance of Runnable
1515
executor.submit(() -> System.out.println("Runnable"));
1616

17-
// tarefa com retorno, instância de Callable
17+
// task with return, instance of Callable
1818
executor.submit(() -> "Callable");
19-
20-
// tarefa que lança uma Exception deve ser Callable, logo deve ter retorno
19+
20+
// task that throws an Exception must be Callable, then needs to return
2121
executor.submit(() -> {Thread.sleep(1); return "Callable";});
2222

23-
// tarefa que lança uma Exception, mas não declara retorno
24-
// NÃO COMPILA pois é interpretada como Runnable
23+
// task that throws an Exception, but declares no return
24+
// NOT COMPILING as it is interpreted as Runnable
2525
executor.submit(() -> Thread.sleep(1));
2626

2727
} finally {

src/org/j6toj8/concurrency/executetasks/Tasks_ShutdownNow.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ public static void main(String[] args) {
1010
ExecutorService executor = null;
1111
try {
1212
executor = Executors.newSingleThreadExecutor();
13-
executor.execute(() -> System.out.println("Thread do Executor: " + Thread.currentThread().getName()));
14-
System.out.println("Thread Principal: " + Thread.currentThread().getName());
13+
executor.execute(() -> System.out.println("Executor Thread: " + Thread.currentThread().getName()));
14+
System.out.println("Main Thread: " + Thread.currentThread().getName());
1515
} finally {
1616
if (executor != null) {
17-
executor.shutdownNow(); // TENTA encerrar todas as threads imediatamente
17+
executor.shutdownNow(); // TRIES to shut down all threads immediately
1818
}
1919
}
2020
// end::code[]

0 commit comments

Comments
 (0)