CONCURRENT PROGRAMMING AND ASPECT ORIENTED PROGRAMMING

来源:互联网 发布:6月进出口数据 编辑:程序博客网 时间:2024/05/28 20:20
utility classes commonly useful in concurent programming. this package includes a few small standardized extensible frameworks as well as some classes that provides useful functionality and are otherwise tedious or difficult to implement. executor is a simple standardized interface for defining custom threading like subsystems, including thread pools , asynchronous io and lightweight task frameworks. depending on which concrete executor class is being used, tasks may execute in a newly created thread., an existing task-execution thread,or the thread calling execute and may execute sequentially and concurrently. executorservice provides a more complete asynchronous task execution framework. an executorservice manages queuing and scheduling of tasks and allows controlled shutdown. the scheduledexectorservice subinterface adds support for delayed and periodic task execution. executorservices provide methods arranging asynchronous execution of any function espressed as callable. in the concurrent programming , there are two basic units of execution: processes and threads. in the java programming language, concurrent programming is mostly concernted with threads. a computer system normally has many active processes and threads. this is true even in systems that only have a single execution core and thus only have one thread actually executing at any given moment. mutual exclusion is needed to ensure . java is one of the most interesting recent developments in concurrent object-oriented programming. as a new language, its creator were able to design a concurrency model within an object-oriented framework withour worrying about backward compatibility issues. the java model integrates concurrency into the object-oriented framework by an adaption of the active object concept. all descendents of the predefined class thread have the predefined methods run and start. a thread is created when its associated object is created. when start is called, the new thread begins it execution by calling the run method. subclassing thread and overriding the run method allows an application to express active objects. alternatively, the run method can be passed to a thread object at object creation time using runnable interface. communication between thread is achieved by reading from and writing to shared objects. of course, these objects need to be protected from simultaneous unpateds in order to avoid interference and subsequent inconsistencies developing in their encapsulated states. in java, every class is implicitly deriveed from the object class which defines a mutual exclusion lock. consequently every object created potentially has its own lock. the methods of a class hat are labeled as synchronized can only be executed when they  have acquired their object's lock. similarly, a synchronized statement nameing  an object can only executed when the object's lock has been obtained. the object class also has methods that implement a simple form of condition synchronization. a thread can wait for  notification of a single event. when used in conjunction with synchronized methods, the language provides a functionality similar to that of a simple monitor. in the java memory model, each thread  is considered to have access to its own working memory  as well as  the main memory that shared between all threads. this working memeory is used to hold copies of the data that resides in the shared main memory. it is an abstraction of data held in registers or data held in  local caches on a multiproccessor system. the jvm transfers data between the main shared memory and thread local memory as and wehn required. it is a requirement that a thread's working memory is invalidated when the thread acquires an objects' lock , that is inside a synchronized methods or statement  any initial read of a shared varible must read the value from main memory. a thread 's working memory is written back to the main memory when the thread releases the lock; that is before a synchronized method or statement finished, any variables written to during the method or statement must be written back to main memory.  many real-time systems have only a limited amount of memory aviable. a more precise, thought not very intersting definition of concurrent programming can be phrased operationally. a java virtual machine and its underlying operation system provides mapppings from apprarant simultaneity to pysical parallelism. most sochet-basee web service for example, http deomons,servlet engines and application serves are multithreaded. the main motivation for supporting multiple concurrent connections is to ensure that new incoming connections do not need to wait out completion of others. many computation-intensive tasks can be parallelized and thus execute more quickly if multiple cpus are present.even on a nominally sequential computers, devices that perform reads and writes on disks,wires,etc, operate independently of the cpu. concurrent programs can use the time otherwise wasted waiting for slow io and can thus make more efficiency use of the a computer's resource. a process is an operating-system abstrattion that allowsone computer system to support many units of execution. each process typically represents  a separate running program. operationg systems guarantee some degree of independence, lack of interference and security.sharing threads may share access to the memory,open files and other resources associated with  a single process.  independence gurantees may be weakened to support cheaper scheduling policies. layering policy control mechanism is a common structuring principle in systems of all sorts. many oo layering and  composition techniques rely on sandwishing some method call or body of code between a given before-action and an after-action. all forms of before after control arrange that a given ground method. every instances of class object and its subclass possesseda lock. scalars of type int,float ,etc are not objects. scalar fields can be locked only via their enclosing objects. individual fields can not be marked as synchronized. locking may be applied only to the use of fields within methods. however, fields can be declared as volatile which affects atomicity,visibility and ordering properties surrounding their use. locking obeys a built-in acquire-release protocol controlled only by use of the synchronized keyword. all locking is block-structured. a lock is acquired on entry to a synchronized method or block and released on exit ,even if the exit occurs due to an exception. locks operate on a per thread, not per-invocation basis. a thread hitting synchronized passes if the lock is free or the thread already possess the lock and otherwise blocks. a synchronized method or block obeys the acquire-release protocol only with respect to other synchronized methods and blocks on the same target object. methods that are not synchronized  may still execute at any time. even if a synchronizeed method is in progress. locking an object does not automatically protect access to static fields of that object's class or any of its superclasses. access to static fields in instead protected viw synchronized static methods and blocks. static synchronization employs the lock possessed by the class object associated with the class the static methods are declared in. the static lock associated with each class is unrelated to that of any other class.,including its supperclasses. the jvm internally obtains and releases the locks for class objects during class loading and initialization. unless you are  writing a special classloader or holding multiple locks during static initialization sequence. actor-based the main reason to use a thread is to create and set into motion a new autonomous,active,process-like object. an aspect is often describled as being a crosscutting structurre. the definition of an aspect is almost as general as that of a class. with aop, an application consists of classes and aspects. an aspect differs from a class in that it implements a crosscutting functionality. an aspect itself is composed of two parts: the pointcut and the advice code. the advice code contains the code to be executed whereas the ponitcut defines the ponits in the program where this code should be implemented. a framework is a set of classes that offers a resuable structure for writing applications. frameworks are used in numerous areas of application development. pointcuts are thus a way of talking about the application. it is often said that a pointcut defines the where of an aspect. on the other hand, the what of an aspect is defined by the advice code.since spring is compliant with the aop alliance api, programming around advice is similiar to programming  around advice in jac and therefore the principles are the same
原创粉丝点击