Java Multithreading Basic summary nine: Mina spy (1)

Source: Internet
Author: User
Tags abstract final sessions volatile

All along, the basic summary of multithreading is out of the application, but to say that the application of multithreading can not but say Mina. As a high-performance Java asynchronous Concurrent Network communication framework, the design and implementation of multithreading in Apache is a good medicine for learning multithreading. On the hand of the Mina source is SVN cut down the latest code, MVN into the Eclipse project after the import of Mina-core source to see the application of multithreading it.

First, simply introduce one of the core interfaces in the Org.apache.mina.core.service package: Ioservice. This interface is the top-level abstraction of both the server-side receive connection and the client-initiated connection, so there is the inheritance and isolation of the ioacceptor and Ioconnector two sub-interfaces. It's flattering to see the finer points of Mina's architecture from this little detail. The most important part of this kind of interface isolation is the accurate division of the abstract behavior within the interface, and the wrong partitioning of the interface will make the implementation appear unreasonable or even wrong. Just don't know whether the designer responsible for abstraction is a one-time abstract success, if it is only to say cow X. As for the interactive session iosession and the actual I/O operations processor Ioprocessor and the underlying processing I/O events iohandle These interfaces are not nonsense, today is looking at the Ioservicelistener-related multi-threaded applications. Ioservicelistener is primarily used to monitor ioservice-related events, and today's protagonist--ioservicelistenersupport is used to put ioservice and corresponding Ioservicelistener a helper class packaged together for management. First look at its source:

Java code

public class Ioservicelistenersupport {
/** the {@link Ioservice} that this instance manages. */
Private final Ioservice service;

/** A List of {@link ioservicelistener}s. * *
Private final list<ioservicelistener> listeners = new copyonwritearraylist<ioservicelistener> ();

/** tracks managed sessions. */
Private final Concurrentmap<long, iosession> managedsessions = new Concurrenthashmap<long, iosession> ();

/** Read only version of {@link #managedSessions}. */
Private final Map<long, iosession> readonlymanagedsessions = Collections.unmodifiablemap (managedsessions);

Private final Atomicboolean activated = new Atomicboolean ();

/** time this listenersupport has been activated * *
private volatile long activationtime;

/** A counter used to store the maximum sessions we managed since the Listenersupport has been * *
private volatile int largestmanagedsessioncount = 0;

/** A Global Counter to count the number of sessions managed since the start * *
Private volatile long cumulativemanagedsessioncount = 0;

/**
* Adds a new listener.
*
* @param listener the added listener
*/
public void Add (Ioservicelistener listener) {
if (listener!= null) {
Listeners.add (listener);
}
}

/**
* @return True if the instance is active
*/
public boolean isactive () {
return Activated.get ();
}

/**
* Calls {@link ioservicelistener#serviceactivated (Ioservice)}
* For all registered listeners.
*/
public void fireserviceactivated () {
if (!activated.compareandset (False, True)) {
The instance is already active
Return
}

Activationtime = System.currenttimemillis ();

Activate all of the listeners now
for (Ioservicelistener listener:listeners) {
try {
listener.serviceactivated (service);
catch (Throwable e) {
Exceptionmonitor.getinstance (). Exceptioncaught (e);
}
}
}

/**
* Calls {@link ioservicelistener#sessioncreated (iosession)} for all registered listeners.
*
* @param session of the session which has been created
*/
public void Firesessioncreated (iosession session) {
Boolean firstsession = false;

if (Session.getservice () instanceof Ioconnector) {
Synchronized (managedsessions) {
Firstsession = Managedsessions.isempty ();
}
}
...

Cumulativemanagedsessioncount + +;
...
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.