Parallel Programming Using mechanisms from concurrent programming to








![Thread. join() public static List<String> download. Pages(String[] urls) { List<Web. Downloader> threads = new Thread. join() public static List<String> download. Pages(String[] urls) { List<Web. Downloader> threads = new](https://slidetodoc.com/presentation_image_h2/ac6e630c5dbeeadd498823d07c5494c6/image-9.jpg)



- Slides: 12
Parallel Programming • Using mechanisms from concurrent programming to improve performance • In some cases, splitting work among threads improves performance • Two cases where overall performance is improved – Multi-core computer – Threads perform I/O
Multi-core Computing • Newer computers have more than one CPU • Java can assign Threads to different CPUs – So Threads actually run at the same time • Performance may improve • To help developers, Java assures that correctness is identical to single CPU case – We still assume Threads take turns – We still assume pre-emptive scheduling
I/O Bound Threads • When a Thread uses a lot of input and output methods, we say it is I/O bound e. g. while(data != EOF){ out. write(data); data = in. read(); } • Each I/O call may take a long time – A long time from the CPUs perspective • The “computer” may be doing work during that time • But actually the CPU is virtually not doing anything!
I/O Bound Threads • If we have to do a lot of I/O, we can improve performance by using multiple threads • That way at least one thread will usually be able to keep the CPU busy with work
extends Thread • Threads execute code from Runnables • As a shortcut we can create classes that extends Thread
extends Thread class A implements Runnable { public void run() { //Do work } } class A extends Thread { public void run() { //Do Work } } . . . A a = new A(); Thread t = new Thread(a); t. start(); . . . A a = new A(); a. start();
Thread. join() • Often a “parent” thread will want to wait for one of its “child” threads to finish • Parent: a thread which calls Thread. start() • Child: the thread created from Thread. start() • Thread. join() – Current thread is suspended until the target thread finishes its run method
Thread. join() class Web. Downloader extends Thread { public String url. String; public String web. Page; public void run() { URL url = new URL(url. String); Input. Stream input = url. open. Stream(); int data = input. read(); while(data != EOF) { web. Page += (char) data; data = input. read(); } } }
Thread. join() public static List<String> download. Pages(String[] urls) { List<Web. Downloader> threads = new Linked. List<Web. Downloader>(); List<String> results = new Linked. List<String>(); for(int i=0; i < urls. length; i++) { threads[i] = new Web. Downloader(urls[i]); threads[i]. start(); } for(int i=0; i < urls. length; i++) { threads[i]. join(); results. add(threads[i]. web. Page); } System. out. println(“All files have downloaded”); }
Random. Access. File • Regular Streams don’t support an efficient “fast forward” operation • Random. Access. File: – Allow read/write to occur at any random position in a file – Adds a “seek” operation public void seek(int byte) – Allows multiple threads to work on different parts of a file in parallel
Random. Access. File • seek allows us to skip to a particular byte in a file • What if our file contains ints, floats, doubles, etc…? boolean: 1 byte int: 4 bytes float: 4 bytes long: 8 bytes double: 8 bytes etc….
Example: Parallel Dot Product • How can we divide the work between m threads?