• No results found

Open your mind to new opportunities

In document Java 8 Multithreaded Programs (Page 39-45)

public static void main(String[] args) {

TimerTask task = new TimerTask() {

Open your mind to new opportunities

With 31,000 students, Linnaeus University is one of the larger universities in Sweden. We are a modern university, known for our strong international profile. Every year more than 1,600 international students from all over the world choose to enjoy the friendly atmosphere and active student life at Linnaeus University.

Welcome to join us!

Bachelor programmes in

Business & Economics | Computer Science/IT | Design | Mathematics

Master programmes in

Business & Economics | Behavioural Sciences | Computer Science/IT | Cultural Studies & Social Sciences | Design | Mathematics | Natural Sciences | Technology & Engineering Summer Academy courses

No tuition-fee for EU-students

JAVA 8:MULTITHREADED PROGRAMS

private static void delay(int time) {

try {

Thread.sleep(time);

}

catch (Exception ex) {

} } }

The TimerTask object which defines the method to be performed, is instantiated based on an anonymous class which inherits the class TimerTask. The class implements the method run(), which is the method used by the timer thread. The Timer object has the type Timer, and the object starts the timer with the statement

timer.schedule(task, 2000);

which means that the task object’s run() method must be carried out after 2 seconds. If you performs the program, the result could be:

Hello world Hello world Hello world Hello world

Alarm: The machine boils...

The TimerTask object which defines the method to be performed, is instantiated based on an anonymous class which inherits the class TimerTask. The class implements the method run(), which is the method used by the timer thread. The Timer object has the type Timer, and the object starts the timer with the statement

JAVA 8:MULTITHREADED PROGRAMS

private static void delay(int time) {

try {

Thread.sleep(time);

}

catch (Exception ex) {

} } }

The TimerTask object which defines the method to be performed, is instantiated based on an anonymous class which inherits the class TimerTask. The class implements the method run(), which is the method used by the timer thread. The Timer object has the type Timer, and the object starts the timer with the statement

timer.schedule(task, 2000);

which means that the task object’s run() method must be carried out after 2 seconds. If you performs the program, the result could be:

Hello world Hello world Hello world Hello world

Alarm: The machine boils...

which means that the task object’s run() method must be carried out after 2 seconds. If you performs the program, the result could be:

JAVA 8:MULTITHREADED PROGRAMS

private static void delay(int time) {

try {

Thread.sleep(time);

}

catch (Exception ex) {

} } }

The TimerTask object which defines the method to be performed, is instantiated based on an anonymous class which inherits the class TimerTask. The class implements the method run(), which is the method used by the timer thread. The Timer object has the type Timer, and the object starts the timer with the statement

timer.schedule(task, 2000);

which means that the task object’s run() method must be carried out after 2 seconds. If you performs the program, the result could be:

Hello world Hello world Hello world Hello world

Alarm: The machine boils...

JAVA 8:MULTITHREADED PROGRAMS tImers

Below is another version of the program where the difference is that the timer is ticking for the first time by half a second and then every second:

JAVA 8:MULTITHREADED PROGRAMS TIMERS

Below is another version of the program where the difference is that the timer is ticking for the first time by half a second and then every second:

package thread13;

import java.util.*;

public class Thread13 {

public static void main(String[] args) {

TimerTask task = new TimerTask() {

public void run() {

System.out.println("Alarm: The machine boils...");

} };

Timer timer = new Timer();

timer.schedule(task, 500, 1000);

for (int i = 0; i < 10; ++i) {

System.out.println("Hello world");

delay(300);

}

timer.cancel();

}

private static void delay(int time) {

try {

Thread.sleep(time);

}

catch (Exception ex) {

} } }

Note how to start the timer, and how to specify the start time and the timer interval for how often the timer should tick. A timer starts a thread which by default is a none-daemon thread. The program terminates therefore, only when the timer is stopped what happens in the last statement in main(). If you performs the program, the result could be:

Note how to start the timer, and how to specify the start time and the timer interval for how often the timer should tick. A timer starts a thread which by default is a none-daemon thread. The program terminates therefore, only when the timer is stopped what happens in the last statement in main(). If you performs the program, the result could be:

JAVA 8:MULTITHREADED PROGRAMS

42

tImers

42

JAVA 8:MULTITHREADED PROGRAMS

42

TIMERS

42

Hello world Hello world

Alarm: The machine boils...

Hello world Hello world Hello world

Alarm: The machine boils...

Hello world Hello world Hello world Hello world

Alarm: The machine boils...

Hello world

A timer can also be started as a daemon thread what the program Thread14 illustrates. The only thing to do is to create the timer as follows:

Timer timer = new Timer(true);

and if you do, the last cancel() statement is unnecessary.

A timer can also be started as a daemon thread what the program Thread14 illustrates. The only thing to do is to create the timer as follows:

JAVA 8:MULTITHREADED PROGRAMS

42

TIMERS

42

Hello world Hello world

Alarm: The machine boils...

Hello world Hello world Hello world

Alarm: The machine boils...

Hello world Hello world Hello world Hello world

Alarm: The machine boils...

Hello world

A timer can also be started as a daemon thread what the program Thread14 illustrates. The only thing to do is to create the timer as follows:

Timer timer = new Timer(true);

and if you do, the last cancel() statement is unnecessary.

and if you do, the last cancel() statement is unnecessary.

JAVA 8:MULTITHREADED PROGRAMS tImers

PROBLEM 1

In this problem, you must write some classes to simulate an alarm that senses one or more thermometers and send messages to different display devices regarding the temperature to which the thermometers shows. You should solve the exercise according to the following guidelines.

1) Start a new project that you can call Alarms. Add the following class to represent a thermometer:

JAVA 8:MULTITHREADED PROGRAMS TIMERS

PROBLEM 1

In this problem, you must write some classes to simulate an alarm that senses one or more thermometers and send messages to different display devices regarding the temperature to which the thermometers shows. You should solve the exercise according to the following guidelines.

1) Start a new project that you can call Alarms. Add the following class to represent a thermometer:

public class Thermometer {

private static final Random rand = new Random();

private int value = rand.nextInt(10) + 15;

public Thermometer() {

...

}

public int getValue() {

return value;

} }

The class has an int variable which represents the temperature, and it is initialized with a random value between 15 and 24 degrees.

You must write a constructor, so it starts a timer that ticks the first time by 1 second and then ticking every second. Each time the timer is ticking, it should add a random value between -5 and 10 to the variable value. That is, the timer changes the temperature – randomly up or down, but mostly up.

2) Add the following interface to the project, which should define the communication between an alarm and a display:

package alarmer;

public interface IDisplay {

public Thermometer getThermometer();

public void show(int temp);

public void warning(int temp);

public void error();

}

The class has an int variable which represents the temperature, and it is initialized with a random value between 15 and 24 degrees.

You must write a constructor, so it starts a timer that ticks the first time by 1 second and then ticking every second. Each time the timer is ticking, it should add a random value between -5 and 10 to the variable value. That is, the timer changes the temperature – randomly up or down, but mostly up.

2) Add the following interface to the project, which should define the communication between an alarm and a display:

JAVA 8:MULTITHREADED PROGRAMS TIMERS

PROBLEM 1

In this problem, you must write some classes to simulate an alarm that senses one or more thermometers and send messages to different display devices regarding the temperature to which the thermometers shows. You should solve the exercise according to the following guidelines.

1) Start a new project that you can call Alarms. Add the following class to represent a thermometer:

public class Thermometer {

private static final Random rand = new Random();

private int value = rand.nextInt(10) + 15;

public Thermometer() {

...

}

public int getValue() {

return value;

} }

The class has an int variable which represents the temperature, and it is initialized with a random value between 15 and 24 degrees.

You must write a constructor, so it starts a timer that ticks the first time by 1 second and then ticking every second. Each time the timer is ticking, it should add a random value between -5 and 10 to the variable value. That is, the timer changes the temperature – randomly up or down, but mostly up.

2) Add the following interface to the project, which should define the communication between an alarm and a display:

package alarmer;

public interface IDisplay {

public Thermometer getThermometer();

public void show(int temp);

public void warning(int temp);

public void error();

}

JAVA 8:MULTITHREADED PROGRAMS

44

tImers

3) As a next step you must to write a class that can represent a display that can show the temperature:

JAVA 8:MULTITHREADED PROGRAMS

44

TIMERS

3) As a next step you must to write a class that can represent a display that can show the temperature:

public class Display extends Thread implements IDisplay {

private Termometer therm;

private String name;

public Display(String name, Termometer therm) {

...

}

public void run() {

...

}

public Thermometer getThermometer() {

return term;

}

public void show(int temp) {

System.out.println(String.format("%d degrees in %s", temp, name));

}

public void warning(int temp) {

System.out.println(String.format("Warning: %d degrees in %s", temp, name));

}

public void error() {

System.out.println("Disaster in " + name + " ...");

...

} }

JAVA 8:MULTITHREADED PROGRAMS tImers

A Display has a name, and shows the temperature of a particular thermometer, and both the name and the thermometer is initialized in the constructor. The class will represent a thread and therefore it inherits the class Thread, and the class’ constructor has to start the thread. The run() method should in principle not do anything but just keep threads alive in a so call busy wait (that is a loop, which performs a short sleep()). The class implements the interface IDisplay and the show() method simply show the temperature, while the method warning() must warn that the temperature is about to be critical (over 80 degrees), and finally the method error() illustrate a disaster situation (when the temperature is 100 degrees). The method error() must also terminate the thread.

4) You must next add a class Alarm:

JAVA 8:MULTITHREADED PROGRAMS TIMERS

A Display has a name, and shows the temperature of a particular thermometer, and both the name and the thermometer is initialized in the constructor. The class will represent a thread and therefore it inherits the class Thread, and the class’ constructor has to start the thread. The run() method should in principle not do anything but just keep threads alive in a so call busy wait (that is a loop, which performs a short sleep()). The class implements the interface IDisplay and the show() method simply show the temperature, while the method warning() must warn that the temperature is about to be critical (over 80 degrees), and finally the method error() illustrate a disaster situation (when the temperature is 100 degrees). The method error() must also terminate the thread.

4) You must next add a class Alarm:

public class Alarm {

private ArrayList<IDisplay> observers = new ArrayList();

private Thermometer[] therms;

public Alarm(Thermometer ... therms) {

...

}

Lighting, beyond

In document Java 8 Multithreaded Programs (Page 39-45)