Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet181/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   177   178   179   180   181   182   183   184   ...   245
Bog'liq
Java 17 Recipes

CHAPTER 10
Concurrency
Concurrency is the ability of a program to execute different (or the same) instructions 
at the same time. A concurrent program can be split up and run on multiple CPUs. By 
making concurrent programs, you take advantage of today’s multicore CPUs. You can 
even see the benefit on single-core CPUs that are I/O intensive. Concurrency is one 
of the toughest topics to handle in modern computer programming; understanding 
concurrency requires the capacity of thinking abstractly, and debugging concurrent 
problems is like trying to pilot an airplane by dead reckoning. Even so, with modern 
releases of Java, it has become easier (and more accessible) to write bug-free 
concurrent code.
This chapter presents the most common need for concurrency tasks—from running 
a background task to splitting a computation into work units. Throughout the chapter, 
you find the most up-to-date recipes for accomplishing concurrency in Java.
10-1. Starting a Background Task
 Problem
You have a task that needs to run outside of your main thread.
 Solution
Create a class implementation that includes the task that needs to be run in a different 
thread. Implement a Runnable functional interface in the task implementation class and 
start a new thread. In the following example, a counter simulates activity, as a separate 
task is run in the background.


352
public class Recipe10_1 {
public static void main(String[] args) {
Recipe10_1 recipe = new Recipe10_1();
recipe.startProcess();
}
private void startProcess() {
Thread backgroundThread = new Thread(new Runnable() {
public void run() {
doSomethingInBackground();
}
},"Background Thread");
System.out.println("Start");
backgroundThread.start();
for (int i= 0;i < 10;i++) {
System.out.println(Thread.currentThread().getName()+": is 
counting "+i);
}
System.out.println("Done");
}
private void doSomethingInBackground() {
System.out.println(Thread.currentThread().getName()+
": is Running in the background");
}
}
If the code is executed more than once, the output should be different from time to 
time. The background thread executes separately, so its message is printed differently 
across each run.
The same code for creating the background thread can be written as follows if you’re 
using lambda expressions.
Thread backgroundThread = new Thread(this::doSomethingInBackground, 
"Background Thread");
Chapter 10 ConCurrenCy


353
The following is the output.
Start
main: is counting 0
main: is counting 1
main: is counting 2
main: is counting 3
main: is counting 4
main: is counting 5
main: is counting 6
main: is counting 7
main: is counting 8
main: is counting 9
Done
Background Thread: is Running in the background

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   177   178   179   180   181   182   183   184   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling