Java Reference
In-Depth Information
need to think in terms of loops at all. The data processing happens internally inside the library.
We call this idea internal iteration . We come back to these ideas in chapter 4 .
As a second pain point of working with collections, think for a second about how you would
process the list of transactions if you had a vast number of them; how can you process this huge
list? A single CPU wouldn't be able to process this large amount of data, but you probably have a
multicore computer on your desk. Ideally, you'd like to share the work among the different CPU
cores available on your machine to reduce the processing time. In theory, if you have eight cores,
they should be able to process your data eight times as fast as using one core because they work
in parallel. [ 5 ]
5 This naming is unfortunate in some ways. Each of the cores in a multicore chip is a full-fledged
CPU. But the phrase “multicore CPU” has become common, so core is used to refer to the
individual CPUs.
Multicore
All new desktop and laptop computers are multicore computers. Instead of a single CPU, they
have four or eight or more CPUs (usually called cores [5] ) . The problem is that a classic Java
program uses just a single one of these cores, and the power of the others is wasted. Similarly,
many companies use computing clusters (computers connected together with fast networks) to
be able to process vast amounts of data efficiently. Java 8 facilitates new programming styles to
better exploit such computers.
Google's search engine is an example of a piece of code that's too big to run on a single computer.
It reads every page on the internet and creates an index, mapping every word appearing on any
internet page back to every URL containing that word. Then, when you do a Google search
involving several words, software can quickly use this index to give you a set of web pages
containing those words. Try to imagine how you might code this algorithm in Java (even for a
smaller index than Google's you'd need to exploit all the cores in your computer).
1.3.1. Multithreading is difficult
The problem is that exploiting parallelism by writing multithreaded code (using the Threads
API from previous versions of Java) is difficult. You have to think differently: threads can access
and update shared variables at the same time. As a result, data could change unexpectedly if not
Search WWH ::




Custom Search