}
// Represents the producer class Producer extends Thread {
private final Shared shared; // reference to the shared resourc public Producer(Shared shared)
{
this.shared = shared;
}
public void run() {
for (char ch = 'A'; ch <= 'Z'; ++ch) shared.setValue(ch);
} }
By thinking about things that nobody has ever thought about before By writing a dissertation about the highest building on earth
With an internship about natural hazards at popular tourist destinations By discussing with doctors, engineers and seismologists
By all of the above
How could you take your studies to new heights?
From climate change to space travel – as one of the leading reinsurers, we examine risks of all kinds and insure against them. Learn with us how you can drive projects of global significance forwards. Profit from the know-how and network of our staff. Lay the foundation stone for your professional career, while still at university. Find out how you can get involved at Munich Re as a student at munichre.com/career.
JAVA 8:MULTITHREADED PROGRAMS waIt() and notIFy()
JAVA 8:MULTITHREADED PROGRAMS wAIT() AnD nOTIFy()
// Represents the consumer class Consumer extends Thread {
private final Shared shared; // reference to the shared resourc public Consumer(Shared shared)
{
this.shared = shared;
}
The program prints a line consisting of the letters from A to Z – in the right order! The comments should explain how the program works, but you should notice a single thing.
The methods getValue() and setValue() in the class Shared performs a wait(). This method is always performed depending on a condition and a call of the method must be done in a loop, such the condition is tested before and after the wait() method. Otherwise there is a risk that the wait() blocks a thread so that it never comes to life again. You should also note that the wait() and notify() are always used in pairs.
EXERCISE 4
You must write a program that you can call ProducerConsumer1. The program has to illustrate the same problem as above, but there must be two differences:
1. the producer must “produce” all characters with code from 33–127 (and not just the characters from A to Z)
2. There must be five consumer threads, and the result could be as shown below:
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRS TUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
The program prints a line consisting of the letters from A to Z – in the right order! The comments should explain how the program works, but you should notice a single thing.
The methods getValue() and setValue() in the class Shared performs a wait(). This method is always performed depending on a condition and a call of the method must be done in a loop, such the condition is tested before and after the wait() method. Otherwise there is a risk that the wait() blocks a thread so that it never comes to life again. You should also note that the wait() and notify() are always used in pairs.
EXERCISE 4
You must write a program that you can call ProducerConsumer1. The program has to illustrate the same problem as above, but there must be two differences:
1. the producer must “produce” all characters with code from 33–127 (and not just the characters from A to Z)
2. There must be five consumer threads, and the result could be as shown below:
JAVA 8:MULTITHREADED PROGRAMS wAIT() AnD nOTIFy()
// Represents the consumer class Consumer extends Thread {
private final Shared shared; // reference to the shared resourc public Consumer(Shared shared)
{
this.shared = shared;
}
The program prints a line consisting of the letters from A to Z – in the right order! The comments should explain how the program works, but you should notice a single thing.
The methods getValue() and setValue() in the class Shared performs a wait(). This method is always performed depending on a condition and a call of the method must be done in a loop, such the condition is tested before and after the wait() method. Otherwise there is a risk that the wait() blocks a thread so that it never comes to life again. You should also note that the wait() and notify() are always used in pairs.
EXERCISE 4
You must write a program that you can call ProducerConsumer1. The program has to illustrate the same problem as above, but there must be two differences:
1. the producer must “produce” all characters with code from 33–127 (and not just the characters from A to Z)
2. There must be five consumer threads, and the result could be as shown below:
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRS TUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
JAVA 8:MULTITHREADED PROGRAMS
38
waIt() and notIFy()
EXERCISE 5
Write a program that you can call ProducerConsumer2, and the program must again illustrate the producer-consumer problem, but this time the class Shared must have three fields:
1. an object of the type Buffer<Integer> – the class from exercise 3 2. a counter, that is an integer that counts from 1
3. a variable sum, that can accumulate a sum
In addition to get methods for the last two variables the class Shared must have two methods:
1. a method insert(), that in the case that the buffer is not full counts the counter up by one, and inserts the value in the buffer
2. a method add(), that in the case where the buffer is not empty, remove a number from the buffer and adds that number to the variable sum
The Producer class, should run in a loop, where it calls the method insert() and places the numbers 1 to 1000000 (both incl.) in the buffer.
The Consumer class should run in a loop and call the method add() that accumulates the sum, and it must run until there is no longer a producer.
Write the program so that there are 10 consumer objects and 5 producer objects, and when the program must complete to print the accumulated sum. If the program is performed correctly, the result shall be:
JAVA 8:MULTITHREADED PROGRAMS
38
wAIT() AnD nOTIFy()
EXERCISE 5
Write a program that you can call ProducerConsumer2, and the program must again illustrate the producer-consumer problem, but this time the class Shared must have three fields:
1. an object of the type Buffer<Integer> – the class from exercise 3 2. a counter, that is an integer that counts from 1
3. a variable sum, that can accumulate a sum
In addition to get methods for the last two variables the class Shared must have two methods:
1. a method insert(), that in the case that the buffer is not full counts the counter up by one, and inserts the value in the buffer
2. a method add(), that in the case where the buffer is not empty, remove a number from the buffer and adds that number to the variable sum
The Producer class, should run in a loop, where it calls the method insert() and places the numbers 1 to 1000000 (both incl.) in the buffer.
The Consumer class should run in a loop and call the method add() that accumulates the sum, and it must run until there is no longer a producer.
Write the program so that there are 10 consumer objects and 5 producer objects, and when the program must complete to print the accumulated sum. If the program is performed correctly, the result shall be:
500000500000
JAVA 8:MULTITHREADED PROGRAMS tImers
7 TIMERS
A timer is an encapsulation of a thread, and a program can start a timer that calls a method from a certain time and possibly let the timer call the method again after a certain interval.
Timers are represented by two classes. The first is the class TimerTask, which defines the method that the timer will perform, and the other class is Timer that is the timer and has methods to start and terminate the timer. The following program starts a timer, and then the program prints a message on the screen every half second. The timer is ticking for 2 seconds and then it stops the program.
JAVA 8:MULTITHREADED PROGRAMS TIMERS
7 TIMERS
A timer is an encapsulation of a thread, and a program can start a timer that calls a method from a certain time and possibly let the timer call the method again after a certain interval.
Timers are represented by two classes. The first is the class TimerTask, which defines the method that the timer will perform, and the other class is Timer that is the timer and has methods to start and terminate the timer. The following program starts a timer, and then the program prints a message on the screen every half second. The timer is ticking for 2 seconds and then it stops the program.
package thread12;
import java.util.*;
public class Thread12 {
public static void main(String[] args) {
TimerTask task = new TimerTask() {