lu.se

Datavetenskap

Lunds Tekniska Högskola

Denna sida på svenska This page in English

A Multithread PDF Downloader

Assignment #2: A Multithread PDF Downloader

Objectives

The main objective of this exercise is to modify the PDF downloader you implemented in the first assignment so that it can run parallel downloads using multiple threads. You will experiment three different thread constructs available in Java: the Thread class, the Runnable interface, and the Executors. The objectives of the second assignment are to:

  • Understand the concept of thread
  • Create a multithreaded program
  • Understand thread constructs in Java

Summary of the Work

Each group will have to:

  • Build a multithreaded version of a download program using:
    • The Thread class;
    • The Runnable interface;
    • The Executors class.
    To help your instructor read your different versions, use a separate package for every thread variant.
  • After your have completed your programs, possibly extend them beyond PDF files.

Reading Assignments

Read the following chapter from the book Java Network Programming by Elliotte Rusty Harold:

  • Chapter 3, on threads.

Read the descriptions of:

of theJava API.

Programming

Creating a Concurrent Downloader

Your first PDF downloader could not handle more than one download at a time. To generate parallel requests to the server, you will start multiple threads of execution in your client.

You will use three different constructs. Please create three different programs, one for each part.

Part I: The Thread class

In this first exercise, you will reprogram your downloader using the Thread class. Here are some hints to design the program. You may decide on a different design. It is OK, provided that you use the Thread class and your program downloads all the files.

  1. Create a Runner class that extends Thread.
  2. Determine the piece of code that carries out the download.
  3. Encapsulate this part of code in a run() method.
  4. Make sure that the Runner class has access to the complete list of URLs to download.
  5. Depending on your program design, a part of your code will need an exclusive access to the URLs, should this code be executed by multiple threads. Encapsulate this piece of code in a method and mark it with the synchronized keyword.
  6. Modify your program so that you can start a given number of threads. Please, do not start more than 10 simultaneous threads (or a reasonable number)
  7. Run your program. You should have the same results as in the first assignment.

Part II: The Runnable interface

In this second program, you will implement the Runnable interface in a new Runner class.

Create a new program that has the same properties as that of Part I and uses this interface.

Part III: Executors

In this third program, you will use Executors. The program structure is different from Parts I and II.

  1. This last program will also use a Runner class, but the launching code will pass the URL to download to the Runner class using its constructor.
  2. Create a pool of Executors allowing a fixed number of threads. Here, you can submit as many tasks as there are PDF files to download in the web page you are considering, as the Executors won't run more threads simultaneously than the pool size. As a consequence, you can pass the URL to the task. Each task will have its own URL and can ignore sharing issues. You will use the submit() method to submit your tasks.

Run your programs, observe the results, and report them to your instructor.

Test them on a few pages and check that they correctly download all the PDF documents these pages contain. Notably, try your programs on this page: http://cs229.stanford.edu/materials.html

Extending your Program (Optional)

Extend your program so that it can handle more document types: images, word documents, etc. You can use the URLConnection methods to determine the documents types