static method ^ free-func-in-namespace

I see no diff. boost thread doc says static yield() is very similar to this_thread::yield(). The this_thread thing is a namespace.

I feel the static method sleep() is very similar to this_thread::sleep().

Advertisements

sybase bcp and identity column

bcp and identity columns are reliable, proven features, but together they surprised me — if I bcp out a table to a file, wipe out the table, then bcp in, occasionally, i see the identity values completely changed. I think it’s because bcp-in ignored identity.

Solution1: use a tool like aqua studio to generate the inserts statements (hopefully not 200,000,000 rows), then enable identity-insert

Solution2: Sybase site says — By default, when you bulk copy data into a table with an IDENTITY column, bcp assigns each row a temporary IDENTITY column value of 0. As it inserts each row into the table, the server assigns it a unique, sequential IDENTITY column value, beginning with the next available value. To enter an explicit IDENTITY column value for each row, specify the -E (UNIX) flag. Refer to the Utility Guide for your platform for more information on bcp options that affect IDENTITY columns.

tibRV transport^connection^session

In JMS, session represents (roughly) a connection, or a socket. More precisely, one or more sessions are created from a single live connection. In JMS, sessions are single-threaded.

In RV, transport is the terminology. Means the same as a session, a socket, a connection to the “network”, to the daemon… Transport is a real object in memory.

In hibernate, session represents a connection to the DB. More precisely, a hibernate session HAS-A real connection object in memory

private/public void run()

What if you want to wrap some existing logic in a public void run() method in your class so you can pass its instance to “new Thread(instance)”, but you don’t like the “public” access to that method? One solution is to wrap that logic in a privateRun() method. When you need to construct a new Thread(instance), you do a


new Thread(){
public void run(){
instance.privateRun(); // anon inner class calling enclosing class's private method
}
}

I think in threading, you end up with lots of nested classes

nested class has special capabilities ] C++^java

Q: Can nested class (NN) methods access a private member of the enclosing class EE? See c++nested class accessing host private member

Can EE methods access NN’s private members? Yes. Tested. See below.

Below is based on P186 ARM, perhaps outdated.
Q: in terms of syntax, how does Inner class methods refer to Outer class’s non-static[1] field?
A: no special access. Unlike java, there’s no hidden “this” field pointing to the Outer _i n s t a n c e_
A: yes if the Inner method has a pointer, reference or a nonref variable of an Outer class _i n s t a n c e_,

Java’s nested class N1 is in the same “private club” along with E2() an Enclose class private method.
– N1 methods can access private members of E2
– E2 methods can access private members of N1
–> Not in C++

Q: So what special capabilities does a nested class has? Why make a class nested at all?
A: to mention the “Inner” type name, you need to prefix it as “Outer::Inner”
A: another specialness — inner class methods can access outer private members. As pointed out on
http://stackoverflow.com/questions/1604853/nested-class-access-to-enclosing-class-private-data-members
— in the original C++98 the inner class has no special privileges accessing the outer class.With C++98 compiler you’d either have to give the inner class the necessary privileges (friendship) or expose
the outer members x and y as public. However, this situation was classified as a defect in C++98, and it was decided that inner classes should have full access to outer class members (even private ones).

[1] static is fine — Outer::f

uninitialized local var and fields – C++/java

C++ variables with static “storage” or global “scope” have default initial value zero, or null for pointers.

c++ fields of primitive types are truly _uninitialized_. C++ need special compiler/debuggers to detect these.

C++ local int objects are truly _uninitialized_ — random initial state. Fixed in java —

java Local variables must be definitely assigned to before they are accessed, or it is a _compile_error_.

java Fields are 0-initialized by _default_. Not in c/c++ .

Note c++ class-type objects are always initialized via one of the (default) constructors. No such thing as an uninitialized MyClass instance.

bond yield — precise definition && example

* yield for a given bond (with a given maturity) is used to discount payout(s) and derive a fair price, aka present value or PV.

* it’s easy to derive price or yield from each other

* yield looks like an annual return, semi-compounded. eg 10% pa, but a coupon rate often looks like $102 pa.

* yield is about the only usable metric to compare bonds across maturities, coupon rates and face values.

* See the posts on discount factor, and how it discounts cash flows. Now, for math-challenged bond traders ;-), we simply assume the discount factor is consistent for every duration. So we discount every income using the same formula —
** cashflow[1] is discounted by 1/(1+10%/2)(1+10%/2) ie twice
** cashflow[1.5] is discounted by 1/(1+10%/2)(1+10%/2)(1+10%/2) ie 3 times
** see link

where is the lock used by Vector synchronization@@

If you know a collection class (Vector for eg) is thread-safe, you can’t assume[1] the synchronization lock(s) used is the Vector instance itself. There could be one or more locks internally employed.

A *real* challenge — create a method that writes[2] to a vector object twice *atomically*. A novice might decide to synchronize on the vector object, with Assumption [1] above. Not wrong, but beware — 2 threads synchronizing on different locks won’t get serialized.

[2] atomic read-then-write is also a challenge, since the write could be based on the read. See Repeated Read in my post on “Transaction Isolation”

ON another note, a local Vector is locked before each of the add operations to ensure that it would not be modified by other threads, but because it is strictly local to the method this is an unnecessary performance hit, and u can’t avoid it with Vectors.

primitive thread communication in practice

Threads can share any data in may ways, but there are some standard patterns to send primitive signals to each other, without any content. These are simple techniques, widely used, battle tested.

* wait/notify [3] – general purpose
* interrupt [1] [3]
* boolean done flag [1]
* join [3]

How about swing?

java.util.concurrent offers a few communication devices. These are not primitive.

[1] these are the 2 standard ways to inform [2] a thread stop itself. See the Thread.stop() api http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html#stop()

[2] from outside the thread ie another thread.

[3] These techniques cause the receiving thread to act immediately. Shared-data techniques need the receiving thread to poll the shared data.

yield^spot rate

(this is less precise than the other posts….)

both rate apply the same 500bps/6month over years

> spot rate covers an entire market (market for cash loan?)
< yield is quoted for a particular bond. More generally, it can describe many bonds of the same credit quality and same tenor, but with different coupon rates.

> spot rate is derived from prices and is a faithful observation/characterization of market sentiment on a given day.
< yield is wishful thinking — hoping interest rate will remain unchanged

yield of a fairly priced 2-year strip should equal SR2 ie today’s sport rate for a 2-year loan.

yield — simplified discount factor

* the concept of yield is a simplification of the precise model of discount factor.

* discount factors for 1 year vs 2 years (DF1, DF2) may look unreasonable. To be precise, you can’t mathematically derive one from the other. For example, Investors should not assume interest rate and inflation will stay constant.

* The simplistic concept of bond yield ignores this strict observation of market reality, by assuming interest rate to remain constant. Under that idealized assumption, DF1*DF1 == DF2.

* I call it wishful thinking. Interest rate is determined by supply/demand and will change.

foundation — discount factor

* discount factor (say 0.8) for 5-year is used to discount any income received on the day 5 years from now. $100 received then is valued at $80 as “present value”. Each periodic receipt from a bond must be discounted by the discount factor for the term. Since this is crucial to the yield concept, let’s be specific.
** payout[1.5] (ie payout in 1.5 years) is discounted by discount factor [1.5] or DF1.5
** payout[2] (ie payout in 2 years) is discounted by discount factor [2] or DF2

* 1-year discount factor is close to 1. The longer the time span, the lower the discount factor, the deeper the discount.

* discount factor is derived from real prices, so reflects supply and demand, and reflects how much investors are/were willing to pay.

* discount factor is primarily a function of “distance” from today (ie wait-period), and not a function of asset class. If you like, the asset in question is liquid cash.

* Discount factor measures “how willing investors are to save”. In high interest/inflation years, discount is deep and heavy. discount factor is lower. investors aren’t willing to put money under their pillow since it devalues too fast.

auto_ptr – G5 operations for a novice

An auto_ptr object wraps a heap[3] object, and deletes it judiciously. You don’t need to delete the heap object.

– If I go out of scope, I delete referent.
– release() ? detach from the referent without deletion, and hold null. A simple but important method.

– reset(p) ? delete current referent, and attach to incoming referent. see [1]. Note referent is a regular ptr, not auto_ptr

– auto_ptr as rval (ie RHS) in copier or assignment? I will hold null after ownership transfer. See [2]

– auto_ptr as lval ? delete current referent (if any), then become owner of the incoming referent, and release the RHS auto_ptr. see [1]

[1] before taking on a new referent object, I always delete current referent to avoid leak
[2] after giving up ownership, I hold null
[3] never wrap a non-heap object, or the delete() will crash

Pitfalls
! not safe as elements in containers — non-standard copier and assignment
! if you independently put the same object into 2 auto_ptr, and one of them subsequently deletes the pointee, making the other a stray dog

standard auto_ptr is a template class …

http://ootips.org/yonat/4dev/smart-pointers.html

c++ stream format flags and manipulators – key words

bitVector — probably there’s a hidden bitArray of boolean flags for each stream instance. http://www.cplusplus.com/reference/iostream/ios_base/flags/

1-to-1 — one flag for one manipulator function

Concrete eg — http://www.cplusplus.com/reference/iostream/manipulators/showpos/

transformer — a typical manipulator is a function accepting a stream by reference, and returning the same stream object by reference

notifyAll() sending event data to waiting threads

Q: notifyAll() wakes up all waiting threads. How nice would it be if you can send some message to them so they know whether to stop waiting?

A: since notifyAll and wait are called on the same object, that object is usually the best message carrier. If you want, you can put in a setNotificationMessage()/getNotificationMessage(). You can use HASA to link this object to any other data object.

asserts for production system diagnosis

I think Assertions were added to java1.4 partly for documentation and partly to help us investigate production systems — restart the system with -ea and you may see error messages before a null pointer exception.

Put another way, without asserts enabled, the error condition (stack trace etc) might be inexplicable and baffling. You enable asserts to let the (production!) system fail earlier so you can see the early signs previously unreported/unnoticed. This is probably faster than reproducing the problem in QA.

Ideally you want to add more logging or error checking, but asserts offer some unique advantages.

overloaded method call resolution — static binding

class Visitor{ // visitor pattern, overloading
void visit(Object o){…} //1
void visit(String s){…} //2
}

If you call
Object o = makeAnObjectOrString(); aVisitor.visit(o), which method runs? I felt it’s resolved at run time. Wrong.

Interviewer pointed out equals(Object o). A novice creates an equals(MyClass o){…}, overloading the inherited equals(Object o). When MyClass is used in an ArrayList, the new method will never be called, because ArrayList.java (see source code) always casts any MyClass instance to Object and calls equals(Object o).

[[Java Precisely]] P44 suggests (1) will be chosen, and always at compile time. At compile time, (2) is ruled out.


public class JavaPreciselyP44 {
static JavaPreciselyP44 instance = new JavaPreciselyP44();
public static void main(String argsp[]) {
//method call won't compile since both methods are equally applicable
instance.print(new ArrayList(), new HashSet());
}
void print(List l, HashSet s) {
System.out.println('o');
}
void print(ArrayList l, Set s) {
System.out.println('s');
}
}

build a stack using a List

An interviewer asked “Sun built a stack data structure using a List. Can you see some serious issues”.

He later pointed out a real LIFO stack only allows access at the top. In the next JDK, how could Sun block random access and still allow all existing applications in the world to continue to function once they upgrade JDK?

Interviewer agreed with me that we can throw an unchecked UnsupportedOperationException, but that will break existing apps after upgrade.

I thought about this idea and still don’t have a conclusion — return some new object to indicate “unsupported operation”? but existing apps won’t notice anything. They will probably function differently after upgrade. Unnoticeable change in system behavior is worse then uncaught exception.

separation of concern: a G3 design priority

for a long time my #1 ideal is testability. Separation of concern is related. Many other related ideals —

* modularize
* layering — extremely popular modularization pattern
* AOP
* stable interfaces between modules
* interchangeable parts
* encapsulation of implementation details

But “separation of concern” is the best phrase. Now specific examples in an environment of multiple systems and teams:
* a basic idea — a table which multiple systems can read/write
** use views rather than the base table
** system to call a proc rather than select/update on the underlying table
* MOM (RV or mq)
* SOA service bus (MOM)
* web service
* producer/consumer
* getter/setters rather than public fields
* declare return types and variables as interface types

FX option – collateral, briefly

A 2008 CFA textbook suggests that most fx options are tailor-made for a given client. Listed fx options trade volume is “fairly low” (2008)

According to Wikipedia, Most of the FX option volume is traded OTC , but a fraction is traded on exchanges like the International Securities Exchange (FX options), Philadelphia Stock Exchange (FX options), or the Chicago Mercantile Exchange (options on FX futures)

I guess one possible reason is – end users (large international organizations who need to convert currencies) don’t like the standardized terms on the exchanges??

Another possible reason is fee??

Most forex option trading is conducted via telephone. There are only a few forex brokers offering online forex option trading platforms.

The same fx option can be viewed as either a call or a put, but the writer is always the writer. Fx option writer (say DB) must put up collateral; fx option buyer must pay the writer an upfront premium. Intuitive for a fx call, but if you see it as a put, the same writer (DB) still has the same collateral/premium deal.

swing/server using 2 http requests (trading

A third party trading platform (like FXAll, tradeweb, Bloomberg, creditex, espeed, brokerTec) could install a swing GUI on traders’ screens. Every time a trader requests info from the remote server, there are 2 swing threads involved —

– one responsible for synchronously uploading the request message, via an http tunnel through firewall
– another responsible for synchronously downloading the response message – http tunnel. I guess this requires a URL including a requestID.

If server is busy or slow, then the 2nd synchronous call should take place after some delay. Otherwise it would block for a long time.

Q: I wonder how server could push market data to swing? I feel the http tunnel means the http client can’t be a server.
%%A: i guess the client makes periodic requests for updates. This is not really real time.

reading FXVol names – simple rules to remember

Confusing rule — a put on AAA/BBB is same as a call on BBB/AAA !

Simple rule: In the beginning, always treat first ccy as commodity and resist the temptation to treat second ccy as commodity. Even though other people tell you about the equivalence, beware the confusion it causes.
Simple rule: on the smile curve or vol surface, use OTM and ATM instruments only. Don’t flip the currency pair.

Simple rule: on the smile curve, you use low low-delta puts and calls, never high-delta instruments, and never flip the pair

Simple rule: option seller is always seller (never becoming buyer) when you convert between an equivalent call and a put. Option seller always collects insurance premium and is always short vega. However, for a RR strategy either counterparty is simultaneously seller+buyer.

C array name vs variables, again

(This applies to C/C++, but how about c# and java? I guess this is an obscure grammar rule that many people just follow without investigating.)

An array name looks like a ptr variable but no no no. Analogue — an array object is a (permanently) allocated “room” in memory; an array name is a (permanent and dominant) name plate on the room door, which a variable never is.

Rule1: Suppose an array name is “myArray”, you can’t put this “myArray” name plate on another room’s door later on.

Rule2: You can’t put another name plate (“myArray2”) on the same door.

This array name is a permanent and exclusive “owner” of this particular room. Conversely, the room permanently belongs to this array name. An “aliasing” pointer Variable can be created for this same address, but compiler won’t allow another array-name as another name plate on this room (Rule2)

Before Variable-Length-Array, array size must be determined at compile time, therefore for a function f(int pa[]), the array-name pa is nonsense. Actually, the compiler translates that to f(int* pa). VLA didn’t change this rule.

See [[c++ primer]].

multicast tibrv ^ JMS #1st take

multicast is not supported in JMS standard. Note JMS (but no multicast?) unicast supports durable subscription.

RV is a good example of multicast. Weblogic JMS also supports multicast.

Multicast (and therefore RV) is more efficient in terms of resources and redundant data transfer.

Q: Unlikely TCP, multicast messages are usually not guaranteed to be once-and-only-once?
A: TIBCO offers a transactional augmentation of RV (RVTX) that guarantees a 1-and-only-once delivery; however, this solution essentially converts RV into a hub-and-spoke system. Consequently, very few RV implementations are transactional.

Q: the JMS synchronous receiver (poller) won’t work. You must use async like onMessage()?
A: i think every receiver must stand ready. A receiver can’t decide when to pull from server.

Q: out-of-sequence is possible in multicast and JMS?
A: both possible

guaranteed delivery (GD) vs NGD

i think GD stipulates exactly one delivery for each message, in expected sequence. Basic expectation in most async transactional environments. For trade booking, that’s the only acceptable delivery. NGD can deliver 0, 1 or many times for the same message. GD requirements include, but is not limited to

* persistent store
* (in the case of pub/sub) durable subscription, so a subscriber can safely go offline
* acknowledgments

Q: if client is offline and comes online, does GD and NGD behave any different?
A: i guess NGD may not deliver all the missed messages.

Q: if volume is high, does GD vs NGD offer any advantage?
A: multicast is most efficient and is usually NGD.

Q: I feel GD is too expensive for broadcast when volume and subscriber counts are high, right?
A: i think so.

Q: what about messages containing other people’s trades executed on the open market?
A: If you are following a particular security and want to track a particular investor’s position, then you need GD. Like a poker game.

another look at swing event handlers

There are 2 distinct threads that interact in very restricted ways. Note the terminology
– METHOD is a compile-time, almostJ
– OBJECT is creation/event time
– CALL is always runtime
C) Thread C CCCreates the (event handler or) listener OBJECT. Often some reference objects (in the form of final arguments) are passed to the creator CALL (not  “method”) at Setup time. Creator passes them to the new event handler OBJECT (not “call”). At Setup time (not event time), event handler OBJECT (not “method” or “call”) has a copy or reference of those reference objects. This is all set up on Thread C. If the event never happen, then the event handler method never becomes a call.
E) Thread E dispatches the EEEvent to the handler OBJECT. In the handler CALL (not “method”), those reference objects are already there, not passed in at event time, and not passed on Thread E. A reference object is like a special device pre-installed in a James Bond car, available at event time.
Note the event handler OBJECT is really a wrapper-over—function-pointer with an invisible-field-for-each-reference-object. Each reference object passed to the creator CALL is stored in the handler OBJECT as an invisible instance field.
An event handler object is stateless and immutable since it can only have invisible fields.
The reference objects hold business data and are actively shared between Thread C and Thread E. Both threads can modify state of those reference objects.
      protected StructureEditAction createOfferingStructureEditAction (final Vector updaterList) {
            // this new() runs on Thread C
            return new StructureEditAction () {
                  // at creation-time, the implicit no-arg ctor saves updaterList as an invisible field. 
                 // This field is never needed until event-time
                  // this method runs on Thread E

                  public void startEdit (KeyEvent e) {

                        Iterator iter = updaterList.iterator ();
                        while (iter.hasNext ()) {
                              Object obj = iter.next ();
                              if (((OfferingUpdate) obj).isUpdatingColumn ((JTable) e.getSource ())) {
                                    ((OfferingUpdate) obj).startEdit (e);
                              }
                        }
                  }

g++ removes a method if never called@@

I suspect the syntax checker in gcc effectively comments out an (potentially illegal) method if it's never called. In the example below,

1) modification of this->intVar is blatant violation of const “this” but this is invisible to the gcc syntax checker unless there's a call to the method.

2) More obviously, bad2() calls a non-existent method but condoned unless someone calls bad2().

using namespace std;
template struct J{
    J(T & rhs){}
    void violateConstThis(T a) const{
       this->intVar = a; // legal when method is const but no one calls.
    }
    void bad2() const{
        this->nonExistentMethod();
    }
    T intVar;
};
int main()
{
    int a=22;
    const J j1(a);
    //j1.violateConstThis(89);
    //j1.bad2();
    return 0;
}

STL iterator ^algorithm, briefly

“iterator is an intermediary between data structures and algorithms.” Here algorithms refer to Comp Science algorithms, so STL algorithms are not an monopoly. You can write your own algorithms but Scott Meyers says no.

STL books showcase stl algorithms but also include DIY algorithm.

Most STL algorithms need iterators [1] as arguments to work on the containers. Algorithms are container-agnostic. Iterators are a device of abstraction. Generic algorithms are presented in terms of generic iterators, not specific containers.

[1] The exact source code of these STL algorithms often mention fake types in place of iterators. When reading these source code or APIs, It’s extremely important to distinguish between real vs fake types.

In Java, Collection.java interface and Collections.java class contain methods applicable on many containers.