Week 6

Format

The course is taught by Peter Schneider-Kamp. Lectures will be on Mondays 10:15-12:00 and Wednesdays 14:15-16:00 in U140 in most weeks. In Week 10, there will be a lecture on Friday 08:15-10:00 in U20.

There are two sections of students: 1st year computer science D1 and D2. These sections will meet for both discussion sections and lab exercises taught by Christian Damsgaard Jørgensen (D1) and Martin Villumsen (D2).

Lecture Monday (02.02.2015)

The first lecture will introduce processes and how to model them using labelled transition systems. Then we will learn two different ways in which thread can be used in Java.

Reading

Chapter 1 and 2

Lecture Wednesday (04.02.2015)

The second lecture will introduce concurrent processes. We will learn both how to model them using labelled transition systems and how to implement them in Java.

Reading

Chapter 3

Exercise

Prepare by looking the information about threads in Java here.

1) MyThread:
Write a program in Java, MyThread, that starts a thread (as a sub-class of the Thread class) which outputs "Running ..." every second (using the Thread.sleep method).

2) MyNamedThread:
Modify the above program such that MyThread gets a String argument in its constructor. Let the main method start three instances of MyThread with different names.

3) MyRun:
Write a program in Java, MyRun, that starts a thread (as an implementation of the Runnable interface) which outputs "Running ..." every second (using the Thread.sleep method). Why is this often a preferable way of constructing threads?

4) MyRunInterrupt:
Modify the above program so that the main thread starts the thread specified above, sleeps for 5 seconds, and then stops it. How did you "stop" the other thread? Are there any other ways of stopping the thread (and why are some approaches not a good idea)?

5) MyRunInterrupt2:
Modify the above program so that the created thread makes some time-consuming computation, for instance:

for (int i=0; i<100000000; i++) {
    /* Waste time creating objects */
    Object o = new Object();
}

Note: you have to take care that your time-consuming computation does not get optimized away (hence the "pointless" creation of objects). How did you then stopĚ this thread from the main thread? Are there any other ways of stopping the thread (and why are some approaches not a good idea)?

Lab

For the implementation tasks, you can find more information about threads in Java here.

1) Labelled Transition System Analyser (LTSA):
Try the LTSA available from the course book home page. You can find most of the examples in our text book, inside File | Examples. We will use the LSTA extensively in the coming weeks.

2) Do Exercises 2.1, 2.3, and 2.4.

3) Do Exercises 3.2 and 3.4.

4) Present the results of Tasks 1-5 from the study group work.

Design by 1234.info | Modified by Peter Schneider-Kamp | CSS 2.0