164x Filetype PDF File size 0.11 MB Source: personales.upv.es
ICCGI 2014 : The Ninth International Multi-Conference on Computing in the Global Information Technology Aspect-Oriented Implementation of Concurrent Processing Design Patterns Shingo Kameyama, Masatoshi Arai, Noriko Matsumoto, Norihiko Yoshida Graduate School of Science and Engineering Saitama University Saitama, Japan {shingo, arai, noriko, yoshida}@ss.ics.saitama-u.ac.jp Abstract—A variety of design patterns are now widely used in This paper is organized as follows: Section 2 gives an software development as their catalog is a collection of knowledge overview of design patterns. Section 3 presents an overview on design and programming techniques and namely elaborated and some functions of aspect-orientated implementation. Sec- patterns. However, as each design pattern is described in the tion 4 introduces an example of aspect-oriented implementa- forms of texts, charts, and simple code examples, it has some tion of GoF design patterns. Section 5 and Section 6 explain limitations in applicability and formal treatment. One of its aspect-oriented implementation of concurrent processing de- reasons is that the design patterns include some crosscutting sign patterns. Section 7 gives some considerations, and Section concerns. To solve this problem, aspect-oriented implementation 8 contains some concluding remarks. of the so-called “Gang of Four” (GoF) design patterns, which are cataloged for component reuse has been proposed. In this paper, wepropose aspect-oriented implementation of design patterns for II. DESIGN PATTERNS concurrent processing, so as to improve and accelerate design and development processes of, for example, network systems, Design patterns are a catalog of typical solutions for some embedded systems, and transaction systems. Our aspect-oriented typical problems in designing and programming in software implementation tailors hierarchical or inclusive relationships development. Erich Gamma, Richard Helm, Ralph Johnson, among design patterns well which are not found in the patterns for component reuse, but found in the patterns for concurrent and John Vlissides, so-called the Gang of Four (GoF), in- processing. troduced 23 design patterns for reuse (so-called the GoF Keywords-Design patterns; aspects; concurrency design patterns) [1]. Design patterns have been getting widely accepted as a technique to improve software development I. INTRODUCTION processes. Many researchers have proposed various design Avariety of design patterns (patterns in short, sometimes, patterns for concurrent processing, real-time processing, and hereafter) [1] are now widely used in software development web applications, etc. as their catalog is a collection of knowledge on design and programmingtechniques and namely elaborated patterns. Their A. Benefits catalog enables novices to refer to experts’ knowledge and Design patterns accelerate software productivity and im- experiences in software design and development. It accelerates proves qualities in the following regards. software productivity and improves qualities as well as it helps communication within a development team. • Aworkload for programming and verification can be Manyresearchershaveproposedvariouspatterns.However, reduced reusing codes in design patterns. patterns have sometimes difficulties in formal or systematic treatment. It is because patterns are described in the forms of • Adevelopment beginner can use it as a guide in which texts in a natural language, charts, figures, and code fragments development experts’ know-how is accumulated. and samples, not in any formal description. • A developer can tell a software design to another To solve this problem, aspect-oriented [2] implementa- developer concisely and precisely by describing the tion of patterns for component reuse has been proposed name of a design pattern. [3][4][5][6][7]. Patterns for component reuse, or sometimes called GoF (Gang of Four) design patterns, were patterns promoting component reuse in object-oriented software [1]. B. Problems Aspects are a technique to modularize codes which are scat- A design pattern has the following problems because its tered to several modules [2]. codes are scattered to more than one module. In this paper, we propose aspect-oriented implementation of concurrent processing design patterns [8][9][10]. Differ- • Adeveloper must understand the structure of a design ent from GoF design patterns, most concurrent processing pattern to apply, extract some necessary codes from patterns use other concurrent processing patterns related to its sample codes, and apply to a program. them, namely, they are not independent to each other. We • It is difficult to maintain or extend the program with implement them by a combination of the implementation of design patterns because codes relating the patterns are related concurrent processing pattern. This approach can be scattered across several modules, and a developer must applied also to aspect-oriented implementation of other design keep all of them in mind correctly. patterns where there are some mutual dependency among them. Copyright (c) IARIA, 2014. ISBN: 978-1-61208-346-9 146 ICCGI 2014 : The Ninth International Multi-Conference on Computing in the Global Information Technology C. Concurrent Processing Design Patterns Thread-per-Message (or Thread-per-Method) Besides GoF design patterns, concurrent processing design It improves responsibility by creating a thread ev- patterns [8][9][10] have been proposed. A concurrent process- ery processing request, and leaving the processing ing design pattern offers a typical solution to the following to the thread. problems in concurrent processing. Worker Thread (or Thread Pool) It improves responsibility, throughput, and capac- • There are problems, such as race conditions and ity by leaving a requested processing to another deadlocks, that do not happen in single threaded thread, and reusing the thread. processing. A race condition happens when more than Future one thread read and write a shared resource without It improves responsibility by enabling a thread mutual exclusion control. A deadlock happens when to receive a result when it is needed, instead of more than one thread lock more than one object and waiting for a result. wait for unlocking with each other. Two-Phase Termination It ensures safety by terminating another thread • Verification is difficult because any problem may or indirectly. may not occur, but possibly not always. Thread-Specific Storage (or Thread-Specific Data) It enables a thread to execute the thread-specific Different from the GoF design patterns for component processing by providing the thread-specific stor- reuse, most design patterns for concurrent processing have age. relations with each other, i.e., they are not independent to each Active Object (or Actor) other, and most patterns use other patterns related to them. In It improves responsibility and enables more than other words, there is an hierarchy among them. We explain one thread to request a processing to a thread- concurrent processing design patterns below. Figure 1 shows unsafe object at a time by leaving the processing the hierarchical relation among them. An arrow denotes that to a single thread. a design pattern on the head side uses a design pattern on the tail side. III. ASPECTS Single Threaded Execution (or Critical Section) Object-oriented programming is a technique that modu- It ensures safety by exclusive control. Only a larizes a concept and a concern as an object, and improves single thread can access a thread-unsafe object at maintainability and extensibility. However, modularization by a time. objects has a limitation because codes of a crosscutting concern Immutable related to more than one module are scattered on them. A It improves throughput by eliminating changing a crosscutting concern is a functionality of the system whose state of an object and exclusive control. definition appear in several classes. Examples of crosscutting Guarded Suspension (or Guarded Waits or Spin Lock) concerns include logging and caching. It ensures safety by blocking a thread until a state of an object changes if a precondition is not met. Aspect-oriented programming [2] is a technique that com- Balking pensates the above-mentioned limit in object-oriented pro- It ensures safety and improves responsibility by gramming and modularizes crosscutting concerns. not executing a processing if a precondition is not met. A. Overview Producer-Consumer It ensures safety and improves throughput by In object-oriented programming, a module calls a method passing an object indirectly. in another module to execute. When a module is added or Read-Write Lock (or Readers and Writers) removed, it is necessary to adjust all the method calls related It ensures safety and improves throughput by to it in all the other modules. Figure 2 shows a class diagram allowing concurrent access for read and requiring depicting this scheme. An arrow between methods expresses exclusive access for write. a processing flow. In aspect-oriented programming, a method or a code frag- Active Object ment specifies positions in other method definitions in any module where it must be called. This method or code fragment Worker Thread Future runs when execution reaches the position. The specification of the position is called a pointcut. The code fragment is called Read-Write Lock Producer-Consumer an advice. This module composed of pointcuts and advices is called an aspect. Guarded Suspension Balking Whenamodule is added or removed, it is not necessary to adjust other modules that call it because there are no explicit Single Threaded Execution method calls related to it. Figure 3 shows a class diagram depicting this scheme. A pointcut and an advice are described Figure 1. Inclusive relation of concurrent processing design patterns. at the bottom part of boxes which represent aspects. A dashed line expresses a specification by a pointcut. Copyright (c) IARIA, 2014. ISBN: 978-1-61208-346-9 147 ICCGI 2014 : The Ninth International Multi-Conference on Computing in the Global Information Technology B. AspectJ A. Aspect-Oriented Implementation of Observer AspectJ is an aspect-oriented language extension adding In object-oriented implementation, the observer is imple- the aspect-oriented features to the object-oriented language mentedasfollows:amethodnotifyObserversiscalled just after Java. Java and AspectJ are used in a related study [3] and any change of a state, and calls linked methods. The following this study. We explain some functions of AspectJ here. codes of observer are defined in more than one module. Figure 4 shows a class diagram of this scheme. Pointcut • NotifyObservers and method calls to execute it. A position that can be specified by a pointcut is limited to a position where a method is called and • Afield and methods to manage linked objects. executed, and where a field value is retrieved or • Asuper-class of linked objects. assigned. Advice In aspect-oriented implementation, observer is imple- An advice includes a before advice, an after mented as follows: the code to execute is defined as an after advice, and an around advice. A before advice advice instead of notifyObservers, and the codes of observer is executed just before processing of the position is modularized in a single aspect. Figure 5 shows a class specified by a pointcut. An after advice is exe- diagram of this scheme. Observer is actually implemented as cuted just after it. An around advice is executed an abstract aspect and a concrete aspect. The abstract aspect instead of it. In an around advice, proceed which defines an advice which does not depend on a target program means execution of the original method can be where a design pattern is applied. The concrete aspect defines specified. a pointcut which depends on it. This improves reusability of Aspect the abstract aspect. Like a class, an aspect can include fields and methods, and can be defined as an abstract aspect B. Benefits or a concrete aspect. An aspect can also inherit There are the following benefits because codes of a design another aspect. pattern are modularized. IV. RELATED WORKS • Adeveloper can apply a design pattern to the position in any target system specified by a pointcut even if he Adesign pattern includes several classes in general, there- or she does not understand its structure. fore a pattern itself is a crosscutting concern. Aspects are • Adeveloperneedtoupdateonlyonemoduleregarding expected to enable modularization of the design pattern as a the design pattern when the target system to which the crosscutting concern. Aspect-oriented implementation of the pattern is applied is updated. GoF design patterns has been proposed in related studies [3]. In this section, we explain aspect-oriented implementation • Thedesignpattern is defined separately from the target of the observer pattern which is one of the GoF design patterns system, therefore it is easier to maintain and reuse the as an example of the related study [3]. The Observer is to system. execute a method whenever a state of another object changes. V. ASPECT-ORIENTED IMPLEMENTATION OF READ-WRITE LOCK ClassB We implemented all the concurrent processing design pat- classA Calls tern mentioned earlier as an abstract aspect and a concrete methodB ClassA aspect like observer, and confirmed that this implementation Calls works correctly. ClassC methodA In this section, we explain aspect-oriented implementation classA methodC1 of read-write lock as an example of our study. A set of locking methodC2 Figure 2. Modularization using objects. Subject observers getSubjectStatus Notifies ClassB notifyObservers Observer addObserver 《aspect》 deleteObserver update methodB Aspect ClassC pointcut advice : methodA ConcreteSubject ConcreteObserver methodC1 getSubjectStatus update methodC2 setSubjectStatus Figure 3. Modularization using aspects. Figure 4. Observer implemented using objects. Copyright (c) IARIA, 2014. ISBN: 978-1-61208-346-9 148 ICCGI 2014 : The Ninth International Multi-Conference on Computing in the Global Information Technology and unlocking is a typical example of crosscutting concern, execution and guarded suspension. Figure 7 shows a class and they are to be done before and after read and write in diagram of read-write lock. read-write lock. It is necessary to apply the aspect of single threaded A. Read-Write Lock Defined Using Objects execution before the aspect of guarded suspension. The first reason is that testing a precondition and notifying a change in In object-oriented implementation, read-write lock is im- state are done under exclusive control. The second reason is plemented by testing a precondition and locking just before that wait and notifyAll are used. An abstract aspect of guarded read and write, and unlocking and notifying a change just after suspension defines a precedence of concrete aspects by adding reading and writing under exclusive control. Figure 6 shows a + after names of the abstract aspects because if a name of a class diagram of this scheme. concrete aspect is used, the abstract aspect need to be updated Locking and unlocking are done by counting reading when adding, deleting, or changing it. and writing threads, and the counters are used for testing a In object-oriented implementation, because a class manages precondition. The precondition for read is that there is no a lock and counters, an instance of the class is created writing thread. The precondition for write is that there is no for every object to be read and written. In aspect-oriented reading and writing thread. A thread waits until the state of implementation, because the aspect manages the lock and the counter changes if the precondition is not met. Exclusive counters. A test of preconditions for reading and writing is control is done by an instance lock that is a feature of Java. The defined in two concrete aspects, and the aspects are defined as instance lock is also necessary to execute wait and notifyAll. privileged to access to the counter which is a private field in another aspect. B. Read-Write Lock Defined Using Aspects Guarded suspension is used for testing a precondition and VI. ASPECT-ORIENTED IMPLEMENTATION OF OTHER notifying a change of the state in read-write lock, and single CONCURRENTPROCESSING DESIGN PATTERNS threaded execution is used for exclusive control in guarded In this section, we present brief summaries of aspect- suspension. This inclusive, or hierarchical relation is shown in oriented implementation of the other concurrent processing Figure 1. design patterns. If a design pattern uses other concurrent In aspect-oriented implementation, we implement the read- processing design patterns, we implemented the design pattern write lock including aspect implementations of single threaded by including the aspects for them as shown in the read-write lock. 《aspect》 We are not successful yet in implementing thread-specific Observer storage in aspects. The Thread-specific storage is a design Subject observers pattern to re-implement an object as an object with the call,target same API, and such a major structural change is difficult to getSubjectStatus after : notifyObservers implement using aspects. addObserver deleteObserver Immutable Notifies Immutable cannot be implemented by an ad- vice because the pattern does not execute any ConcreteSubject ConcreteObserver code. Instead, We implemented a checking feature getSubjectStatus update whether a field of a target object is assigned from setSubjectStatus outside the object or not. Figure 5. Observer implemented using aspect. Reader Reader 《aspect》 sharedResource sharedResource SingleThreaded Execution Reads Reads 《aspect》 call,target ReadWriteLock ReadWriteLock around SharedResource SharedResource : proceed readWriteLock Uses readLock call { guarded } read readUnlock before : readLock writeLock read after : readUnlock 《aspect》 write writeUnlock write call Guarded before : writeLock Suspension1,,2 Writes Writes after : writeUnlock call,target ,, Writer Writer before : wait call,target sharedResource sharedResource after : notifyAll Figure 6. Read-Write Lock using objects. Figure 7. Read-Write Lock using aspects. Copyright (c) IARIA, 2014. ISBN: 978-1-61208-346-9 149
no reviews yet
Please Login to review.