Notes by Dr. Xi
Displaying notes 61 - 70
Created by Dr. Xi on April 28, 2011 08:58:03
Last update: April 28, 2011 08:58:03
The java.util.concurrent.atomic package is a small toolkit of classes that support lock-free thread-safe programming on single variables .
This is an example of a shared counter without synchronization or lock:
import java.util.concurrent.atomic.AtomicInteger;
...
Created by Dr. Xi on April 27, 2011 15:58:31
Last update: April 27, 2011 20:05:22
From JavaDoc : java.util.concurrent.locks.ReentrantLock is a reentrant mutual exclusion Lock with the same basic behavior and semantics as the implicit monitor lock accessed using synchronized methods and statements, but with extended capabilities. The extended capabilities are: tryLock() : acquire the lock if possible, otherwise immediately return false. tryLock(long timeout, TimeUnit unit) : acquire the lock within the timeout, return false after timeout. lockInterruptibly() : try to acquire the lock and enter wait state, but can be interrupted by other threads and exit waiting. The following shows how java.util.concurrent.locks.ReentrantLock can be used. Start two threads to manipulate the same counter, one incrementing, the other decrementing. Since the counter is incremented and decremented the same number of times, in the end, it should be 0. Without locking,...
Created by Dr. Xi on April 27, 2011 11:57:36
Last update: April 27, 2011 11:58:35
This is a sample struts-config.xml file for Struts 1.x .
<?xml version="1.0" encoding="UTF-8"?>
<!DO...
Created by Dr. Xi on April 27, 2011 11:26:54
Last update: April 27, 2011 11:27:13
With DTD:
DocumentBuilderFactory df = DocumentBuilderFactory...
With Schema:
// Create schema
SchemaFactory xsdFactory = Sch...
With Relax NG:
// Create schema
SchemaFactory xsdFactory = Sch...
Created by Dr. Xi on April 26, 2011 21:25:55
Last update: April 27, 2011 11:08:57
The following code validates a web.xml file against the web-app 2.5 schema:
// Java XML validation with schema
import java....
web.xml used for testing:
<?xml version="1.0" encoding="UTF-8"?>
<web-ap...
According to Java API doc , validation can also be done while parsing by calling setSchema on the parsing factory. However, validation doesn't work for the SAXParserFactory!
// Java XML validation with schema
import java....
Created by Dr. Xi on April 27, 2011 08:28:31
Last update: April 27, 2011 08:37:40
JBoss example with hsql:
<persistence>
<persistence-unit name="myapp"...
MySQL example with JDBC :
<persistence
xmlns="http://java.sun.com/xml...
With OpenEJB transaction manager:
<persistence version="1.0"
xmlns="...
With EclipseLink :
<?xml version="1.0" encoding="UTF-8"?>
<persist...
Created by Dr. Xi on April 19, 2011 16:02:55
Last update: April 19, 2011 16:03:24
Method Description object.__getattr__(self,name) Called when an attribute lookup has not found the attribute in the usual places (i.e. it is not an instance attribute nor is it found in the class tree for self ). object.__setattr__(self,name,value) Called when an attribute assignment is attempted. This is called instead of the normal mechanism (i.e. store the value in the instance dictionary). If __setattr__() wants to assign to an instance attribute, it should not simply execute self.name = value — this would cause a recursive call to itself. Instead, for new style classes, it should call the base class method with the same name, for example, object.__setattr__(self, name, value) . For classic classes, it should insert the value in the dictionary of instance attributes, e.g., self.__dict__ [name] =...
Created by Dr. Xi on April 19, 2011 16:01:39
Last update: April 19, 2011 16:01:39
This note relates to Python 2.x. A Python class is old-style by default, unless it has another new style class or the "top level" class object as its parent. The sure way to tell that an object is an instance of a new style class is to use the function type , type(x) returns <type 'instance'> for an old-style class, but it returns <class 'ClassType.X'> for a new-style class.
Class definition:
class A: # old style class
def __init__(sel...
Test session:
>>> A
<class ClassType.A at 0x7f36ae442fb0>
...
Created by Dr. Xi on April 18, 2011 12:10:37
Last update: April 18, 2011 12:14:24
Python defines a long list of special methods to customize class behavior. This is a short list for the Basic Customizations . Method Description object.__new__(cls[, ...]) Called to create a new instance of class cls. __new__() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. __new__() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation. object.__init__(self[,...]) Called when the instance is created. The arguments are those passed to the class constructor expression. If a base class has an __init__() method, the derived class’s __init__()...
Created by Dr. Xi on April 17, 2011 21:34:20
Last update: April 17, 2011 21:34:20
#!/usr/bin/python
# -*- coding: latin-1 -*-
...