Java Threads
Operating Systems Lab Class 3
In this lab session we will get hands on the basics of thread programming in Java . You will learn how to create threads and how to wait for them to terminate (see the example in Appendix 1). We will also exploit the data parallelism pattern to speed up the processing of input data.
1 Exercise (a): Primes
We are going to write a program that computes the Prime numbers with the help of the Sieve of
Eratosthenes.
The Sieve of Eratosthenes, named after its inventor, the Greek scholar Eratosthenes (276–194
BC), provides a way to find all prime numbers less than or equal to some fixed number n. To
construct it, write out all integers from 2 to n. Then cross out all multiples of 2 except 2 itself,
then all multiples of 3 except 3 itself, and so forth for each number that has not been crossed out
yet up to
n.
Example: n = 27. The multiples of 2 are crossed out with a /, the multiples of 3 with a \, and the multiples 5 with a –.
1.1 Getting Started
1. Download the os-lab3a.zip archive from Canvas and unzip it into your workspace.
2. Start NetBeans and open the project. The project should contain two files in the src directory:
Primes.java and PrimesTask.java.
3. Compile and run the code. This should work without problems.
1.2 Implementation
5. Complete the two Java classes. Using the Sieve of Eratosthenes algorithm, write a threaded program that outputs prime numbers. The program will work as follows:
• Running java Primes 27 should compute the prime numbers for n = 27, and output them, each prime number on a separate line.
• Parse the number n from the command line arguments (see usage()) method provided in Primes.java.
• Check the command line arguments and call usage() whenever the arguments are not good.
• Pass the number n to the PrimesTask that you start as a thread in the main() method.
When the thread has terminated, output all prime numbers. 1
√
n. The numbers that are not crossed out in the end are the prime numbers from 2 to
1.3 Reflection
1. Does the threaded implementation speed up the computation of the prime numbers? 2. In which situation is it advantageous to compute them in a thread?
2 Exercise (b): Letters
We will write a program that detects which letters ’a’ . . . ’z’ occur in a given set of words. 1. Download the os-lab3b.zip archive from Canvas and unzip it into your workspace.
2. Start NetBeans and open the project. The project should contain two files in the src directory: Letters.java and LettersTask.java.
3. Complete the two Java classes. The program will work as follows:
• Running java Letters Hello World should produce the following output:
a: false
b: false
c: false
d: true
e: true
f: false
g: false
h: false
i: false
j: false
k: false
l: true
m: false
n: false
o: true
p: false
q: false
r: true
s: false
t: false
u: false
v: false
w: false
x: false
y: false
z: false
• In the main() method create threads that execute LettersTask for each word passed as command line argument.
• When all threads have finished, output the result as shown above.
2.1 Reflection
1. Which kind of parallelism does your implementation exploit?
3
2.
3.
Extra question: Implement a sequential version of the program and compare the runtimes of the sequential (single-threaded) and the multi-threaded version. Hint: You need very, very long input strings to be able to measure the difference.
Extra question: If we wanted to count the number of occurrences of each letter what would we have to consider?
Java Thread Library API References
http://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html
http://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html
Appendix 1: Java Thread Library
The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. The steps of creating a new thread of execution are as follows,
• Declare a class that implements the Runnable interface
• Then implement the run method in that class (override the run() method)- e.g.,
• Create a thread: an instance of the class can then be allocated, passed as an argument when creating Thread
• Start the thread running- e.g.,
Download the HelloThreads.zip archive from Canvas and unzip it into your workspace. Start NetBeans and open the project. Running java HelloThreads should produce the fol-lowing output:
Thread States in Java
Threads can be in one of five states: New, Ready, Running, Blocked, or Finished