Implementing and Testing Producer-Consumer Problem Using Aspect-Oriented Programming
Implementing and Testing Producer-Consumer Problem Using Aspect-Oriented Programming
Abstract—Producer-Consumer problem is a classical and function codes and nonfunctional codes, which are easy to lead
representative synchronization problem in the field of computer code-scattering and code-tangling. It is not beneficial to the
science, which can be implemented by object-oriented development and maintenance of the software.
programming language. However, the implementation of object- Aspect-Oriented Programming (AOP) was first proposed in
oriented programming often leads to be tangled between [3] as a programming technique for modularizing concerns that
functional codes and synchronization codes, which are easy to cross-cut the basic functionality of programs. The aim of AOP
lead code-scattering and code-tangling. Aspect-oriented is to resolve the code-scattering and code-tangling and
programming (AOP) has been proposed as a technique for modularize the crosscutting concerns. The crosscutting
improving separation of concerns for the final software. This
concerns include security, logging, exception handling and
paper aims to resolve concrete aspect and implement the
synchronization of producer-consumer based on AOP under two
synchronization etc. Many distinguish work has done to deal
ways: single buffer and multiple buffers, and the execution result with the discrete aspect. Zhu[4] implements the producer-
shows that AOP can resolve the producer-consumer problem consumer problem based on JAC that implements the
effectively. Furthermore, the execution time of AOP program is concurrency based on the extended JAVA comments, but the
shown to contrast with that of object-oriented programming, JAC needs the specific compiler.
which shows that AOP can almost get the same execution time Though much work has done in the specific aspect, there is
with object-oriented programming, while AOP can gain the less work on the producer-consumer problem using AOP. As
modularization better than object-oriented programming. the producer-consumer problem is a representative problem in
synchronization, the solution will help to the software design
Keywords—Producer-Consumer problem; synchronization; and programming.
Aspect-oriented programming; separation of concerns This paper takes the classical producer-consumer problem
as an example to provide the solution to the synchronization
I. INTRODUCTION using AOP. In section 2, the implementation of producer-
consumer problem is presented. In order to present explicitly,
In the field of computer science, the producer-consumer the implementation using single buffer and multiple buffers are
problem is a classical example of the multi-process shown separately. In section 3, the comparison of execution
synchronization problem. Synchronization is an important and time is done between OOP and AOP. Section 4 concludes the
familiar problem in the design and development of the software, paper.
and many applications use synchronization as their method and
settlement, such as Synchronous Product [1] and FlexSync[2] etc.
When multiple processes or threads access the critical resource, II. IMPLEMENTATION USING AOP
synchronization is needed to be control.
Nowadays, many object-oriented programming languages This section presents the implementation of producer-
have supported the synchronization and can implement the consumer problem using AOP under two manners: single
producer-consumer problem. For example, Java programming buffer and multiple buffers.
language implements the synchronization through the keyword A. Description of the Producer-Consumer Problem
synchronized, as the prefix of the method, that allows only one
The problem describes two processes, the producer and the
thread enters the synchronized code at the same time and avoid
consumer, who share a common, fixed-size buffer. The
abusing the critical resource. Java can also control the
producer's job is to generate a piece of data, put it into the
communication among the thread by the methods: wait(),
buffer and start again. At the same time the consumer is
notify() or notifyAll(). The method wait() keeps the thread
consuming the data at a time. The problem is to make sure that
waiting until the method notify() notify it to end the waiting.
the producer won't try to add data into the buffer if it's full and
Besides Java, many object-oriented programming (OOP)
that the consumer won't try to remove data from an empty
languages have resolved the synchronization well. However,
buffer.
the implementation of OOP leads to be tangled between the
750
After removing the code related synchronization, the result of public aspect SynAspect {
abstraction is shown in Figure 4. Seen from it, there is a class //the number of blocked consumers
private int getter;
CubbyHole we defined. The method get() is charged of getting // the number of blocked producers
the product from the buffer, and method put() is responsible to private int putter;
put the product into the buffer. The buffer size is four. pointcut syncPut(): call(void CubbyHole.put(int,int));
before(): syncPut(){
class CubbyHole { synchronized(this){
// the buffer length is four while(CubbyHole.count>=4){
private int[] goods=new int[4]; putter++;
// the point of first producer try {wait();} catch (InterruptedException e) {}
private int front; }
// the point of last producer }
private int rear; }
// the number of products in the buffers after() returning: syncPut(){
public static int count; synchronized(this){
public CubbyHole(){ if(getter>0){
front=0; getter--;
rear=0; notify();
} }
public synchronized int get(int id) { }
front=(front+1)%4; }
System.out.println("Consumer #" + id + " got: " + pointcut syncGet():call(int CubbyHole.get(int));
goods[front]); before(): syncGet() {
count--; synchronized(this){
return goods[front]; while(CubbyHole.count<=0){
} getter++;
public synchronized void put(int value, int id) { try {wait();} catch (InterruptedException e) {}
rear=(rear+1)% 4; }
goods[rear]=value; }
System.out.println("Producer #" + id + " put: " + }
goods[rear]); after() returning: syncGet(){
count++; synchronized(this){
} if(putter>0){
} putter--;
notify();
Figure 4. The result of abstracting synchronization code }
}
After abstracting the synchronization code, the next step is }
to construct a synchronization aspect that includes joint point, }
pointcut and advice. The synchronization aspect that named as Figure 5. The implementation of synchronization aspect
SynAspect is shown in Figure 5. Joint point, being the place
where pointcut captures, is usual selected in some specific
methods. Here is the method put() and get(). In the definition of
the pointcut, the head of put() and get() method is needed and
the method begin with the name of class CubbyHole as shown
in line 6 and 23 of Figure 5.
Advice is also an important part of an aspect. Advice
includes before advice, after advice and around advice. Before
advice and after advice are implemented for every pointcut. In
figure 5, we present the before advice and after advice of
pointcut, named as syncGet() and syncPut(). For example, the
task of before advice of pointcut syncGet() is to keep the
producer waiting when the buffer is full. The task of after
advice of pointcut syncGet() is to notify the consumer to
consume the product when there are some consumers waiting
for the product.
The execution result is shown in Figure 6 with the tool
Eclipse 3.3 and AspectJ. AspectJ allows Java programmers to
divide program code into separate pieces, each of that describes
a different aspect. These pieces are merged at join points by the
AspectJ compiler into a single regular program. Conclusion
from Figure 6, AOP can resolve the producer-consumer
problem.
751
III. EXPERIMENTATION Eclipse 3.3 and AspectJ’s Eclipse plug-in:
The execution time is compared between AOP and OOP. In ajdt_1.5.2_for_eclipse_3.3. We separately test the execution
the experimentation, the program includes four threads: two time according to the OOP and AOP implementation in
producer threads and two consumer threads. The hardware and different execution times. As the experimentation is performed
software environment is as following. In the aspect of hardware, in multi-thread environment and the call of thread is uncertain,
the frequency of CPU is Intel Core™ 2 Duo T5600 1.83GHz we performed eight times for in different execution times-5, 50,
and the capacity of memory is 1G. In the aspect of software, 500 and 5000. The result of execution time is shown in TABLE
operating system is Windows XP, and the software uses I.
752