Java Session 2

toaststicksrifleΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 5 χρόνια και 1 μήνα)

265 εμφανίσεις



University of Ulster

Faculty of Computing and Engineering

1

Java Session
3

Java Session
3
-

Busy
-
Waiting

Introduction

Of the three main types of main
-
memory synchronisation, busy
-
waiting is the
oldest. Java has very
good
concurrency facilities
, so normally, busy
-
waiting is not
used
, but even so there are times when a busy
-
waitin
g solution gives the
programmer fine control over what happens.

First of all,
note that in this session
you

are provided with a class called
Lock

which has two methods,

tSet
, and
free()
,
which have the
effects

described in the
lecture
. See
the code
below
.

/*


* 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 pro
ject
lockpoems

illustrates the Critical Section problem. The two threads
JackAndJill

and
Flowers

run concurrently, and attempt to access a critical region.

The idea is that only one thread
is allowed to be executing in its critical section at
any one time.

Exercise

1

1. Download the zip file, extract the

code

files, fire up Netbeans and open
lockpoems

as a project in Netbeans.

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

and
jackAndJill.

3. Note that the shared variable
lock

is used in a

busy
-
waiting entry protocol to
each critical section.

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

5.

(Medium

Hard
)

Try to modify the code so that one thread spends a very short
time in the NON
-
Critical Section and has very short intervals between checking
the lock. Get this thread to crowd out the other thre
ad, preventing it from getting
into the Critical Section.



University of Ulster

Faculty of Computing and Engineering

2

Java Session
3

/*


* Main.java


*/

package lockpoems;


public class Main {




public static void main(String[] args) {



Lock lock = new Lock();


Flowers flower = new Flowers(lock);


JackAndJ
ill 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


D
elay.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 Session
3

/*


* JackAndJill.java


*/

package lockpoems;

public class JackAndJill extends Thread {


Lock lock;


JackAndJill(Loc
k 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.o
ut.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 Session
3



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




University of Ulster

Faculty of Computing and Engineering

5

Java Session
3

Critical Section
Entry Using

New Method in Lock Class

Note that we don't really need to write our CS Entry
code in the programs, we can
just write a n
ew method for the
Lock
class:



public void CSEnter() {


while (tSet()) {


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


Delay.skip(0.5);


}


}


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

Remember the car park
example 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 lecture
we had the general
implementation for the coars
e
-
grained atomic action where a process waits till
B

is true then
executes
S
.


CSEnter()
;

while ( not
B

) {


CSExit()
;


skip
;


CSEnter();

}

S;

CSExit();


See the Deep Dark Castle Example….




University of Ulster

Faculty of Computing and Engineering

6

Java Session
3

Exercise

2

1. Consider the statement from the
Car

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 project
carparkbusy

and run the program. Note that
you can end up with up
to five cars in the carpark at the same time.


4. Go to the
run()

method of the
Car

class and add the statements from Step 2 into
the 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 Session
3

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("datsu
n", 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) {


thi
s.name = name;


this.spaces = spaces;


this.lock = lock;


}



University of Ulster

Faculty of Computing and Engineering

8

Java Session
3


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 carp
arkbusy;


public class Spaces {


public int
numSpaces = 0;




public Spaces(int mySpaces) {


numSpaces = mySpaces;


}

}


/*


* Lock.java


*/


package carparkbusy;


public class Lock {


boolean value = false;




public synch
ronized boolean tSet() {


boolean retValue = value;


value = true;


return retValue;


}


public void free() {


value = false;


}



public void CSEnter() {


while (tSet()) {


System.out.println("Wait
ing on lock...");


Delay.skip(0.5);


}


}



public void CSExit() {


free();




}

}



University of Ulster

Faculty of Computing and Engineering

9

Java Session
3

Exercise 3 (Hard)

Write a program to simulate the five Dining Philosophers. Each philosopher has a
cycle of thinking and eating.
There is a shared bowl of spaghetti and a set of five
chairs arranged around the table. Five forks are placed on the table, so that each
philosopher 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 each
fork down and leaves the table in order to think.

Hint: you need to use more than one lock…