Σχόλια 0

Το κείμενο του εγγράφου

University of Ulster

Faculty of Computing and Engineering

1

Java Session3

Java Session3-

Busy-Waiting

Introduction

Of the three main types of main-memory synchronisation, busy-waiting is theoldest. Java has verygoodconcurrency facilities, so normally, busy-waiting is notused, but even so there are times when a busy-waiting solution gives theprogrammer fine control over what happens.

First of all,note that in this sessionyou

are provided with a class calledLock

which has two methods,

tSet, andfree(),which have theeffects

described in thelecture. Seethe codebelow.

/*

* Lock.java

*/

public class Lock {

boolean value = false;

public synchronized boolean tSet() {

boolean retValue = value;

value = true;

return retValue;

}

public void free() {

value = false;

}

}

A Busy-Waiting Solution for the Critical Section Problem

The projectlockpoems

illustrates the Critical Section problem. The two threadsJackAndJill

andFlowers

run concurrently, and attempt to access a critical region.

The idea is that only one threadis allowed to be executing in its critical section atany one time.

Exercise

1

1. Download the zip file, extract the

code

files, fire up Netbeans and openlockpoems

as a project in Netbeans.

2. Have a good look at the code and identify the critical sections in the two threads,flowers

andjackAndJill.

3. Note that the shared variablelock

is used in a

busy-waiting entry protocol toeach critical section.

4. Each thread prints out either the poem (when inside the Critical Section), orsome dots or some plussigns (when outside the Critical Section).

5.

(Medium

Hard)

Try to modify the code so that one thread spends a very shorttime in the NON-Critical Section and has very short intervals between checkingthe lock. Get this thread to crowd out the other thread, preventing it from gettinginto the Critical Section.

University of Ulster

Faculty of Computing and Engineering

2

Java Session3

/*

* Main.java

*/

package lockpoems;

public class Main {

public static void main(String[] args) {

Lock lock = new Lock();

Flowers flower = new Flowers(lock);

JackAndJill jAndJ = new JackAndJill(lock);

flower.start();

jAndJ.start();

}

}

/*

* Flowers.java

*/

package lockpoems;

public class Flowers extends Thread {

Lock lock;

Flowers(Lock x) {

super("Flowers");

this.lock = x;

}

public void run() {

while (true) {

// CSEnter-

enter Critical Section

while (lock.tSet()) {

System.out.println("Flowers waiting on lock...");

Delay.skip(0.5);

}

// Critical Section

Delay.idleUpTo(1.0);

System.out.println("\t\t\t1. Roses are red,");

Delay.idleUpTo(1.0);

System.out.println("\t\t\t2. Violets are blue,");

Delay.idleUpTo(1.0);

System.out.println("\t\t\t3. Sugar is sweet,");

Delay.idleUpTo(1.0);

System.out.println("\t\t\t4. And so are you.");

// CSExit-

exit Critical Section

lock.free();

// Non-critical section

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

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

Delay.idleUpTo(0.5);

}

}

}

University of Ulster

Faculty of Computing and Engineering

3

Java Session3

/*

* JackAndJill.java

*/

package lockpoems;

public class JackAndJill extends Thread {

Lock lock;

JackAndJill(Lock x) {

super("Jack and Jill");

this.lock = x;

}

public void run() {

while (true) {

//Enter Critical Section

while (lock.tSet()) {

System.out.println("Jack and Jill waiting on lock...");

Delay.skip(0.5);

}

//Critical Section

Delay.idleUpTo(1.0);

System.out.println("\t\t\tj1. Jack and Jill");

Delay.idleUpTo(1.0);

System.out.println("\t\t\tj2. Went up the hill");

Delay.idleUpTo(1.0);

System.out.println("\t\t\tj3. To fetch a pail of water.");

Delay.idleUpTo(1.0);

System.out.println("\t\t\tj4. Jack fell down");

Delay.idleUpTo(1.0);

System.out.println("\t\t\tj5. And broke his crown");

Delay.idleUpTo(1.0);

System.out.println(

"\t\t\tj6. And Jill came tumbling after.");

//Exit Critical Section

lock.free();

// Non-Critical Section

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

System.out.println("++++");

Delay.idleUpTo(2.0);

}

}

}

}

University of Ulster

Faculty of Computing and Engineering

4

Java Session3

NB: To stop the program from running then you should right-click onlockpoems(run) and choose to stop it.

University of Ulster

Faculty of Computing and Engineering

5

Java Session3

Critical SectionEntry Using

New Method in Lock Class

Note that we don't really need to write our CS Entrycode in the programs, we canjust write a new method for theLockclass:

public void CSEnter() {

while (tSet()) {

System.out.println("Waiting on lock...");

Delay.skip(0.5);

}

}

The Car Park Problem Using <await (B) S;>

Remember the car parkexample from Exercises 1. We have the general solution:

Program

Car() {

while (true) {

<await

(numSpaces > 0) numSpaces = numSpaces-

1;

>

… Park car, go shopping

<numSpaces = numSpaces +

1;>

… Drive home, unpack shopping

}

}

Program

CarPark {

int numSpaces =2;

co

Car(i = 1 to 5)

oc

}

General Solution (Implementation of <await (B) S;>

Remember that in the lecturewe had the generalimplementation for the coarse-grained atomic action where a process waits tillB

is true thenexecutesS.

CSEnter();

while ( notB

) {

CSExit();

skip;

CSEnter();

}

S;

CSExit();

See the Deep Dark Castle Example….

University of Ulster

Faculty of Computing and Engineering

6

Java Session3

Exercise

2

1. Consider the statement from theCar

program

<await

(numSpaces > 0) numSpaces

= numSpaces-

1;

>

This is in the form

<await (B) S: >

Plug the expressions for B and S; into the following:

CSEnter()

while ( not B ) {

CSExit()

skip;

CSEnter()

}

S;

CSExit();

2. Also consider:

<numSpaces = numSpaces +

1;>

This is just a Critical Section, so you can code it with:

CSEnter()

numSpaces = numSpaces +

1;

CSExit();

3. Using Netbeans, open the projectcarparkbusy

and run the program. Note thatyou can end up with upto five cars in the carpark at the same time.

4. Go to therun()

method of theCar

class and add the statements from Step 2 intothe correct place in the code.

You will have to use:

lock.CSEnter();

spaces.numSpaces = spaces.numSpaces + 1;

lock.CSExit();

University of Ulster

Faculty of Computing and Engineering

7

Java Session3

5. Do the same for the statements in step 1:

Here you should use something like:

lock.CSEnter();

while (not (numSpaces > 0

) {

lock.CSExit();

Delay.skip(1.0);

lock.CSEnter();

}

numSpaces = numSpaces-

1;

lock.CSExit();

/*

* Main.java

*/

package carparkbusy;

public class Main {

public static void main(String[] args) {

Lock lock = new Lock();

Spaces spaces = new Spaces(2);

Car c1 = new Car("volvo", lock, spaces);

Car c2 = new Car("datsun", lock, spaces);

Car c3 = new Car("polo", lock, spaces);

Car c4 = new Car("clio", lock, spaces);

Car c5 = new Car("lotus", lock, spaces);

c1.start();

c2.start();

c3.start();

c4.start();

c5.start();

}

}

/*

* Car.java

*/

package carparkbusy;

public class Car extends Thread {

private String name;

private Spaces spaces;

private Lock lock;

public Car(String name, Lock lock, Spaces space) {

this.name = name;

this.spaces = spaces;

this.lock = lock;

}

University of Ulster

Faculty of Computing and Engineering

8

Java Session3

public void run() {

Delay.idleUpTo(3.0);

// <await (numSpaces > 0) numSpaces = numSpaces-

1;

System.out.println(name + " in car park.");

Delay.idleUpTo(6.0);

System.out.println(" " + name + " about to leave.");

// < numSpaces = numSpaces + 1; >

System.out.println(" " + name + " has left." );

}

}

/*

* Spaces.java

*/

package carparkbusy;

public class Spaces {

public intnumSpaces = 0;

public Spaces(int mySpaces) {

numSpaces = mySpaces;

}

}

/*

* Lock.java

*/

package carparkbusy;

public class Lock {

boolean value = false;

public synchronized boolean tSet() {

boolean retValue = value;

value = true;

return retValue;

}

public void free() {

value = false;

}

public void CSEnter() {

while (tSet()) {

System.out.println("Waiting on lock...");

Delay.skip(0.5);

}

}

public void CSExit() {

free();

}

}

University of Ulster

Faculty of Computing and Engineering

9

Java Session3

Exercise 3 (Hard)

Write a program to simulate the five Dining Philosophers. Each philosopher has acycle of thinking and eating.There is a shared bowl of spaghetti and a set of fivechairs arranged around the table. Five forks are placed on the table, so that eachphilosopher must share forks with his companions to the left and right.

In order to eat a philosopher must obtain both

forks. Having eaten he puts eachfork down and leaves the table in order to think.