- Higher-Order JavaScript
A JavaScriptish companion to Mark-Jason Dominus’s Higher-Order Perl
(categories: javascript books toread programming reference )
All posts by ajohnson
Bulk import of email addresses into Movable Type notifications
One of the blogs on my Movable Type installation is actually using the notifications feature built into Movable Type (which gives you the ability to let people subscribe to a blog via email) and needed to do a bulk import of email addresses that he already had in csv format. Unfortunately, Six Apart doesn’t provide a way to do a bulk import so I dug into google. Because the install is backed by MySQL, the solution was to use the LOAD DATA INFILE command, which reads a csv file from disk and loads the data into a table you specify. In case someone else ever needs to do this with Movable Type, the syntax I ended up using looked like this:
LOAD DATA LOCAL INFILE 'contacts.csv' INTO TABLE mt_notification FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (notification_email,notification_blog_id,notification_name)
and my contacts.csv file looked like this:
ajohnson@cephas.net,10,Aaron Johnson ...
Also interesting to note that according to the MySQL documentation, the LOAD DATA INFILE command is usually 20 times faster (!) than using separate INSERT INTO statements.
Links: 9-21-2006
- Stu Downes ยป Folksonomy in the enterprise
Supposedly “…as the number of people who tag increases above 50 you quickly cease producing unique tags.” Interesting.
(categories: tagging tags enterprise content folksonomy ) - ISGM Music: The Concert Podcast
Boston museum makes concert series performances available for free via podcast. They encourage sharing. Awesome.
(categories: music podcasts podcast classical free mp3 art )
JDJ: Mailets and Matchers
Remember back a couple months ago when I posted an article about using Apache James to implement VERP? Well, it actually did end up getting published in JDJ. If you subscribe to the magazine, you should have it in your hot little hands right now or you can visit the archives. If not, you’ll have to wait until next month when the article becomes available for free.
Links: 9-19-2006
- Tom McQueeney Weblog – Java Blog
Notes from a webinar on Subversion best practices.
(categories: subversion sourcecontrol bestpractice ) - MOO | Share your contact info and your flickr photos
Really cool non-business business cards.
(categories: photos flickr minicards )
Links: 9-13-2006
- RollerWiki: Proposal Atlas
Roller proposal to improve the front-page of roller to support the needs of a community of communities.
(categories: community blogging roller planet communities blogs ) - RollerWiki: Roller_3.0_WhatsNew
Roller 3.0 released with some spiffy new features.
(categories: blogging roller templating urlsupport ) - Blogs and Community ? launching a new paradigm for online community? at The Knowledge Tree
From the article: “… looks at some of the emerging patterns of blog based communities and raises some questions for their strategic application.”
(categories: community onlinecommunities communities blogging blogs for:barrytallis for:billjive research ) - Ruby: Rite
The next version of Ruby as per Matz’s slides at RubyConf 2003. via joecheng.wordpress.com
(categories: ruby rite ruby2.0 nextgen )
Links: 9-8-2006
- Macintosh Home Automation and Control Server
OS X home automation: turn on your lights via email. Now if only someone would develop something that will wash the dishes and put them in the dishwasher using email…. that I’d buy.
(categories: homeautomation x10 mac osx insteon remotecontrol home ) - Flickr: The Info Graphics Pool
Infoviz pool on flickr. via plasticbag.org
(categories: infoviz information graphing flickr visualization )
Links: 9-7-2006
- Intuitive Navigation with Tag Clouds | Performancing.com
Read the comments for some interesting analysis.
(categories: tags tagging blogging blogs navigation archives clouds tagclouds )
Links: 9-6-2006
- Amazon.com: Top Wikis
Amazon has wiki pages for some (all?) of it’s products.
(categories: wiki amazon marketing community )
AtomicInteger
A couple weeks ago I came across some code that used an instance of an AtomicInteger, which is part of the java.util.concurrent.atomic package, which I believe the majority of which was written by Doug Lea, author of the excellent util.concurrent library. I wrote the class name down in my todo.txt thinking that it would be interesting to find out how AtomicInteger (and all the other atomic related classes) give you the ability to increment a shared integer in a multi-threaded environment, but I didn’t get around to it until this past weekend. If you’re not familiar with the atomic classes in JDK 5.0, the big takeaway is that they “…support lock-free thread-safe programming on single variables”. In code that means that if you have this:
public final class Counter {
private long value = 0;
public synchronized long getValue() {
return value;
}
public synchronized long increment() {
return ++value;
}
}
you should be using something more like this:
public class NonblockingCounter {
private AtomicInteger value;
public int getValue() {
return value.get();
}
public int increment() {
int v;
do {
v = value.get();
while (!value.compareAndSet(v, v + 1));
return v + 1;
}
}
Regardless of your familiarality, herewith are some notes on my research:
- I downloaded the source code for JDK 5.0 and popped open the source for AtomicInteger, skipping down to the
getAndIncrementmethod. The source for that method is curious, to say the least. It starts with a for loop that never ends, retrieves the current int value, adds one to that value and then fires thecompareAndSetmethod, only returning if thecompareAndSetmethod returns true. It took me a couple minutes to wrap my brain around this small block of code: no locking and what looks like an endless loop. Here it is if you don’t feel like downloading the code yourself:public final int getAndIncrement() { for (;;) { int current = get(); int next = current + 1; if (compareAndSet(current, next)) return current; } }What’s remarkable about this block is that it seemingly ignores the fact that it was designed to be accessed simultaneously by multiple threads… and in fact that’s exactly how it was designed. If this method were a conversation between a person and a machine, it might go something like this:
Person: Get me the current value!
Machine: 1
Person: Add one to that value!
Machine: 2
Person: If 1 is equal to the current value, then set the current value to 2, otherwise, let’s do it all over again!
Machine: Try again.
Person: WEEE! Let’s try again!I think the exclamation points are apt in this case because it turns out that the atomic classes use algorithms that are sometimes called ‘optimistic’ (which is probably not a trait that you ascribe to most programmers unless they’re talking about schedules) but are described on Wikipedia (and probably elsewhere) as ‘lock-free’ or ‘wait-free’ algorithms. They’re optimistic in the sense that they assume the best case:
I’m sure that no other threads will be accessing the method when I’m using the method. Even if they do, I don’t care, I’ll just try again (and again and again)…
Optimistic. The pessimistic version assumes the worst case:
I’m sure some other thread will be trying to access the method when I’m using the method. I’ll have to block them from using it every time I invoke this method.
If you check out the Wikipedia article I linked to above, you’ll see the implementation of these lock-free algorithms use atomic primitives, the notable one being the ‘compare and swap‘; the
getAndIncrementmethod on the AtomicInteger class in Java has a correspondingcompareAndSetmethod, which is backed by a class called Unsafe, which itself doesn’t have publicly available documentation. However, if you download the JDK 5.0 source code like I did and peek at it, you’ll see a method that looks like this:public final native boolean compareAndSwapInt(Object o, long offset, int expectedValue, int newValue);
And that’s where the source code adventure stopped. If I’m understanding the method signature correctly, it means Sun has a native library (written in C?) for every OS that sets the value of the Object o at memory location ‘offset’ to value ‘newValue’ if the current value is equal to ‘expectedValue’.
- I found a really really great article on IBM developerWorks by Brian Goetz1 called “Going atomic” (you’ll probably recognize the
CounterandNonBlockingCounterexamples I posted above). In it he does a great job describing compare and swap and the difference between a lock-free and wait-free implementation and then, just to show off, shows some benchmarking graphs that compare synchronization, ReentrantLock, fair Lock and AtomicLong on an 8-way Ultrasparc3 and a single-processor Pentium 4.You should also read his articles on developerWorks called More flexible, scalable locking in JDK and Introduction to nonblocking algorithms.
- Dave Hale wrote an article that discusses the use of AtomicIntegers in a multi-threaded environment called “Parallel Loops with Atomic Integers“. He also pointed out an article published in the March 2005 edition of Dr. Dobb’s Journal called “The Free Lunch Is Over: A Fundamental Turn Toward Concurrency in Software“.
Hope you learned something today!
1: I saw Brian speak at the No Fluff Just Stuff Symposium in Boston last fall. He’s as good in person as he is on paper. Go hear him speak if you have a chance.