Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
COMP2121 Lab 3
Transactional Memory and Mutual Exclusion
The goal of this lab is to understand the difference between the concept of Transactional Memory
and Mutual Exclusion. This requires that you set up Synchrobench as explained in Lab 1. It is
recommended to use a machine with at least 2 cores running Java 6 or 7 and a Unix-based operating
system, like Linux, Solaris or Mac OS X.
Exercise 1: Lock-based Dining Philosophers
Consider five philosophers who spend their lives thinking and eating. The five philosophers are seat-
ing at a circular table with a bowl of rice in front of each and they share five chopsticks as depicted
in Figure 1. From time to time a philosopher gets hungry and tries to pick up the two chopsticks that
are the closest to her (the chopsticks that are between her and her left neighbor and between her and
her right neighbor). A philosopher may pick up only one chopstick at a time and cannot pick up a
chopstick that is already in the hand of another philosopher. When a philosopher has both chopsticks
at the same time, she starts eating until she’s no longer hungry. When she is finished eating, she puts
down both of her chopsticks and starts thinking again.
Figure 1: The dining philosopher problem; 5 philosophers sharing 5 chopsticks to eat.
Download the file http://sydney.edu.au/engineering/it/~gramoli/doc/ens/7-mutex/DiningPhilosopher.java.
Try to understand the proposed solution to the aforementioned dining philosopher problem. What is
the problem of such a proposal?
Make each dining philosopher wait 100ms (use Thread.sleep()) between the times at which it
acquires the two locks of the getChopsticks method and extends the execution to 20 seconds by
changing the value of numSeconds. What do you observe when executing the updated program?
Duration: 10 min
1
COMP2121 Transactional Memory and Mutual Exclusion
Exercise 2: Transactional Memory
Replace the lock-based synchronisation of DiningPhilosopher.java by transactional memory:
remove all locks and add the transactional @Atomic annotation to the method that needs to execute
atomically. Make sure to import the org.deuce.Atomic annotation and compile it with the proper
classpath referring to the bin folder of Synchrobench Java (compiled in Lab 1).
1 cd synchrobench-master/java
2 javac -cp bin DiningPhilosopher.java
In the same folder, run this transactional version of the dining philosopher program with proper JVM
arguments as follows:
1 java -Dorg.deuce.exclude="java.util.*,java.lang.*,sun.*" \
2 -javaagent:"lib/deuceAgent-1.3.0.jar" \
3 -Dorg.deuce.transaction.contextClass=org.deuce.transaction.estm.Context
4 -cp lib/compositional-deucestm-0.1.jar:lib/mydeuce.jar:bin \
5 -Xbootclasspath/p:lib/rt_instrumented.jar \
6 DiningPhilosopher
What do you observe? Add a verification to the code that prints an error if the chopsticks have been
grabbed and released by another thread right before the thread releases them.
What should we guarantee now to make sure that no philosophers starve? Propose a solution.
Duration: 20 min
Distributed Systems and Network Principles Page 2 of 2