0% found this document useful (0 votes)
13 views

Implementing and Testing Producer-Consumer Problem Using Aspect-Oriented Programming

Uploaded by

Vaibhu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Implementing and Testing Producer-Consumer Problem Using Aspect-Oriented Programming

Uploaded by

Vaibhu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

2009 Fifth International Conference on Information Assurance and Security

Implementing and Testing Producer-Consumer Problem Using Aspect-Oriented


Programming

Yang Zhang Jingjun Zhang Dongwen Zhang


College of Information Science & Engineering Scientific Research Office College of Science
Hebei University of Science and Technology Hebei University of Engineering Hebei University of Science & Technology
Shijiazhuang, Hebei, P.R. China Handan, Hebei, P.R. China Shijiazhuang, Hebei, P.R. China
[email protected] [email protected] [email protected]

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

978-0-7695-3744-3/09 $25.00 © 2009 IEEE 749


DOI 10.1109/IAS.2009.41
The solution for the producer is to go to sleep if the buffer public aspect SynAspect {
is full. The next time the consumer removes an item from the pointcut syncPut(): call(void CubbyHole.put(int,int));
before(): syncPut(){
buffer, it wakes up the producer who starts to fill the buffer synchronized(this){
again. In the same way the consumer goes to sleep if it finds while(CubbyHole.available==true){
the buffer to be empty. The next time the producer puts data try {wait();}
into the buffer, it wakes up the sleeping consumer. catch (InterruptedException e) { }
}
B. Implementation using Single Buffer CubbyHole.available=true;
}
In this mode, there is only one buffer that can be used by }
producer and consumer. Once the producer produces a product, after() returning: syncPut(){
he puts it into the buffer and waits for the consumer to consume synchronized(this){ notify;}
the product. The state of the buffer is full. After the consumer }
has consumed the product, he will be blocked and wait for the pointcut syncGet():call(int CubbyHole.get(int));
producer to produce, then the buffer is null. before(): syncGet() {
synchronized(this){
The implementation code of class CubbyHole, that includes while (CubbyHole.available == false) {
the buffer, is shown in figure 1. In class CubbyHole, the try {wait();}
variant goods act the role of the buffer and the variant available catch (InterruptedException e) {}
is used to judge whether the buffer is full or not. }
CubbyHole.available=false;
class CubbyHole { }
// this is the buffer }
private int goods; after() returning: syncGet(){
// the state of the buffer, the default value is false that means the synchronized(this){ notify;}
buffer is null }
public static boolean available=false; }
// consume the product from the buffer
public synchronized int get(int id) { Figure 2. The source code of synchronization Aspect
System.out.println("Consumer #" + id + " got: " + goods);
return goods;
}
//put the produce into the buffer, and the parameter value is the
number of the produce.
public synchronized void put(int value, int id) {
goods=value;
System.out.println("Producer #" + id + " put: " + goods);
}
}

Figure 1. The source code of Class CubbyHole

The source code of Class CubbyHole, as shown in figure 1,


doesn’t include the code that related to the synchronization
control, such as communication between producer and
consumer. The synchronization control is abstracted to be an
aspect that is shown in figure 2. In aspect SyncAspect, two
pointcuts and its advice are built. The two pointcuts have the
different advice. Every advice includes the before advice and
after advice.
Figure 3. The execution result using single buffer
The programming tools that we employed is the Eclipse 3.3
and the AspectJ[5 that are used to execute the program. The
AspectJ plugin ajdt_1.5.2_for_eclipse_3.3 is used to C. Implementation using Multiple Buffers
collaborate with Eclipse to get the result. The execution result In this mode, there are multiple buffers between producer
is shown in figure 3. Besides the code as shown in figure 1 and and consumer. The producer can continue to put the product
figure 2, some other threads including of producer thread, into the buffers until the buffer is full, while the consumer
consumer thread and main thread is built in the program. We continues to get the product from the buffer until the buffer is
employ two producer instance (be signed as 1 and 2) and two null. Different from the single buffer, this way needs more
consumer instance (be signed as 1 and 2) in the program. Seen communications between producer and consumer.
in the figure 3, when the producer produces a product, the During the implementation of multiple buffers, the main
consumer consumes the product. Conclusion from the figure 3, work is to identify the crosscutting concerns, which need to
the producer and consumer can be synchronized. separate from the functional parts and to be modularized as an
aspect. As existing shared resource (the buffers) and the
producer or the consumer need to access the buffer,
synchronization is separated and abstracted to be an aspect.

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.

Figure 6. The execution result using multiple buffers

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.

TABLE I. COMPARISON BETWEEN OOP AND AOP IN DIFFERENT MODE


times Implementation using single buffer Implementation using multiple buffers
time(ms) OOP AOP OOP AOP
NO. 5 50 500 5000 5 50 500 5000 5 50 500 5000 5 50 500 5000
1 0 15 125 688 16 47 125 656 0 16 94 579 16 47 157 563
2 16 15 110 641 31 31 140 578 16 16 125 500 16 31 125 516
3 15 16 125 687 15 31 141 594 0 15 141 531 15 47 125 516
4 0 15 125 657 31 32 125 609 16 31 94 516 15 31 125 562
5 0 32 110 672 16 47 125 610 0 16 109 500 31 47 110 547
6 15 31 109 687 16 31 140 563 0 32 109 500 16 31 125 516
7 0 15 125 640 47 31 125 594 16 31 94 516 15 47 125 531
8 16 16 109 672 15 32 156 578 0 31 110 500 32 31 141 562

As shown in TABLE I, the execution time of AOP is very


close to that of OOP. When the execution times is very few(eg. ACKNOWLEDGMENT
5 times), the execution time of OOP may be distinct from that It is a project supported by Natural Science Foundation of
of AOP. For example, the execution of OOP is zero sometimes Hebei Province under Grant No.F2009000852, P.R. China, and
while the execution of AOP is not zero. Sometimes the Science-Technology Foundation of Hebei Province under
execution time of AOP is less than that of OOP. We can Grant No.07215601D-3, P.R. China.
conclude that the execution effective of AOP is not declined,
though AOP needs to capture the joint point and execution the REFERENCES
advice. AOP can obtain almost the same execution time with
object-oriented programming, but using AOP can gain the [1] Carlos Canal, Pascal Poizat, and Gwen Salau¨n. “Model-Based
modularization better than OOP. Adaptation ofBehavioral Mismatching Components.” IEEE transactions
of software Engineering. 2008 Vol.34. No.4. 546-562.
[2] Charles Zhang, “FlexSync: An aspect oriented approach to Java
IV. CONCLUSION synchronization”, 31st International Conference on Software
Engineering, Vancouver, Canada, May, 2009
The main contribution of this paper is that the producer and
[3] G. Kiczales, J. Lamping, A. Mendhekar, C. Maeda, C.Lopes, J.-M.
consumer problem is implemented using AOP and the Loingtier, J. Irwin. “Aspect-Oriented Programming”, in Proceedings of
execution time of AOP is comparison with that of OOP. The the 11th European Conference on Object-Oriented Programming,
result shows that AOP is the supplement of OOP. AOP can Finland,Springer-Verlag, 1997, pp. 220-242.
obtain the separation of concerns and make the function parts [4] H. Zhu, Z.L. Zhao. “To solve the producer-consumer problem based on
more reuse and functional cohesion without losing efficiency. the JAC”. Journal of Computer Science. 2006, Vol. 33, No.5, pp. 292-
294. (In Chinese)
Our work will benefit to the development and maintenance of
the software that related to the synchronization. [5] G. Kiczales, E. Hilsdale, J. Hugunin, M. Kersten, J. Palm, W.G.
Grisworld. “Getting started with AspectJ”. Communications of the ACM,
2001, Vol.44, No.10, pp59-65.

752

You might also like