
Main Menu

User Info
 Welcome Anonymous

Top Ranked Feeds

Random Feeds
|
View the feed - binkley's BLOG
Main - Uncategorized - Feeds that are not yet Categorized - binkley's BLOG
[Comments | Printer Friendly Page | Send to a Friend | Is this your feed/content? | Feature this Feed ]
- A future for Java after all?
One of my favorite bloggers, Alex Miller, opens today with Apparently Mark Reinhold announced that closures would be added to JDK 7 at Devoxx today. When I realized JDK7 would have no closures, I mentally wrote Java off as a language destined to be COBOL or FORTRAN or C: important in a practical way, but not to be where my heart was. (I'm looking around; Scala is likely my next language of choice.) And, lo! Closure—in Java! Java may remain after all the language I prefer most. What a surprising turn. UPDATE: Stephen Colebourne posts more details.
- Frankie says, don't do it (but does anyway)
Aleksander Kmetec explains Acme::Dont implemented in Scala. Is it scarier that I can follow along, or that I recognize the Perl don't pragma? (Naturally Damian Conway is to blame.) Funny I ran across this post while reading Bytecodes meet Combinator: invokedynamic on the JVM.
- How are your algorithms?
A fabulous post by Peteris Krumins summarizing MIT's Introduction to Algorithms lectures. It's like having a little Knuth in your browser.
- A new programming language: Go
Time to brush up my resume and add a new programming language: Go, say 3 years experience? I chuckle as I type this, but I have interviewed candidates for my present employer with an equally tenuous grasp on Java at variance with their resumes.
- More mana from the Czech lands: IntelliJ IDEA open-sourced
Take THAT, Eclipse! UPDATE: This announcement is so popular, the JetBrains site is down. Never seen that before.
- Surprisingly, there really is more to life than programming
Since I started this blog I have stuck consistently to programming topics. But I feel like posting a touch more broadly. Here are some of my favorite links: And when I'm not browsing the Internet, I write classical music, cook freestyle, and thoroughly enjoy Rebecca, Greg and Ben.
- Keeping a current timestamp in a Java concurrent map
Tricker than I like: In Java, how do you keep a map with the values being the "most recent timestamp" for the key? How do you do this for a concurrent map with multiple threads updating? boolean isLatest(final K key, final long newTimestamp,
final ConcurrentMap<K, Long> map) {
Long oldTimestamp = map.putIfAbsent(key, newTimestamp);
// If we are first, there is no previous to compare
// with
if (null == oldTimestamp)
return true;
// Make sure we are still the most recent
while (newTimestamp > oldTimestamp) {
if (map.replace(key, oldTimestamp, newTimestamp))
// No other thread has updated, we are most
// recent
return true;
// Another thread got here first, recheck
oldTimestamp = map.get(key);
}
// Another thread put in a newer update, we are not
// most recent
return false;
} As with all multi-threaded coding, bugs are easy to make and hard to spot. If I have made one, please let me know! UPDATE: Fixed a typo. Thanks, David!
- Examples for maven
Ching and Porter post nice examples for build-helper and antrun maven plugins (part 2 of series; part 1 here).
- Posts I missed: Higher-order Java parallelism
An extended article in four parts I missed first time last year: - Parallel Strategies and the Callable Monad
- Parallel List Transformations
- Threadless Concurrency With Actors
- A Better Future
Very apropos for me as we are presently evaluating functional libraries for Java.
- Fast reflection, a cool post
"Coding Masters" (sorry, I didn't find his name) posts an interesting dissection of reflection and tunes until it is as quick as normal code. This is one for my toolbox.
- More on favorite idioms
lambdaj does a much better job than I did: List<Integer> biggerThan3 = filter(greaterThan(3), asList(1, 2, 3, 4, 5));
- Qi4j 1.0-RC1
Qi4j realizes much of the promise of annotations. Mixins and aspects without code or bytecode generation, what's not to love?
- Design, Don't do it
Are you designing a thread-safe front to a single-threaded resource? Relax. The wrong way Write layers and layers of complexly interacting abstraction over an object pool, letting the object pool (hopefully) protect you from threading concerns. The right way Use the JDK: public class FooService {
private final UnsafeResource resource;
private final ExecutorService service;
public FooService(final UnsafeResource resource) {
this.resource = resource;
service = createPoolFor(resource);
}
public <T, E extends Exception> Future<T> submit(
final Work<T, E> work) {
return service.submit(new Callable<T>() {
T call() throws E {
return work.use(resource);
}
});
}
public interface Work<T, E extends Exception> {
T use(final UnsafeResource resource) throws E;
}
// Good place for an extension point depending on your needs
private static ExecutorService createPoolFor(final UnsafeResource resource) {
return newSingleThreadExecutor(new ThreadFactory() {
public Thread newThread(final Runnable r) {
return new Thread(r,
FooService.class.getSimpleName() + ": " + resource);
}
});
}
}
- Comments on Hipp's A Lesson In Low-Defect Software
Thanks to Alecco Locco for useful comments on Hipp's A Lesson In Low-Defect Software. the full title: A Lesson In Low-Defect Software -or- A Journey From A Quick Hack To A High-Reliability Database Engine and how you can use the same techniques to reduce the number of bugs in your own software projects Those comments got me to read the paper.
- A favorite functional idiom in Java
Ok, two favorite functional idioms in Java: public abstract class Option<T>
extends Cursorable<T> {
public static <T> Option<T> some(final T value) {
return new Option<T>() {
@Override
public int size() {
return 1;
}
@Override
public T get() {
return value;
}
};
}
public static <T> Option<T> none() {
return new Option<T>() {
@Override
public int size() {
return 0;
}
@Override
public T get() {
throw new IllegalStateException();
}
};
}
public abstract int size();
public abstract T get();
public final boolean isEmpty() {
return 0 == size();
}
@Override
public final Iterator<T> iterator() {
return new Iterator<T>() {
private boolean empty = isEmpty();
@Override
public boolean hasNext() {
return !empty;
}
@Override
public T next() {
if (empty)
throw new NoSuchElementException();
empty = true;
return get();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}
public abstract class Cursorable<T>
implements Iterable<T> {
public static <T> Cursorable<T> over(
final Iterable<T> it) {
return new Cursorable<T>() {
@Override
public Iterator<T> iterator() {
return it.iterator();
}
};
}
public final Cursorable<T> select(
final Select<T> select) {
return new Cursorable<T>() {
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
private final Iterator<T> it
= Cursorable.this.iterator();
private Option<T> current = none();
@Override
public boolean hasNext() {
while (it.hasNext()) {
final T next = it.next();
if (!select.accept(next))
continue;
current = some(next);
return true;
}
current = none();
return false;
}
@Override
public T next() {
if (current.isEmpty())
throw new NoSuchElementException();
return current.get();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
public final <U> Cursorable<U> project(
final Project<T, U> project) {
return new Cursorable<U>() {
@Override
public Iterator<U> iterator() {
return new Iterator<U>() {
private final Iterator<T> it
= Cursorable.this.iterator();
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public U next() {
return project.on(it.next());
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
public interface Select<T> {
boolean accept(final T input);
}
public interface Project<T, U> {
U on(final T input);
}
} And a demo: public class SelectAndProjectMain {
public static void main(final String... args) {
for (final Integer number : over(asList(1, 2, 3, 4))
.select(new Cursorable.Select() {
@Override
public boolean accept(final Integer input) {
return 0 == input % 2;
}
})) {
System.out.println("Even: " + number);
}
for (final String number : over(asList(1, 2, 3, 4))
.project(new Cursorable.Project() {
@Override
public String on(final Integer input) {
return Integer.toBinaryString(input);
}
})) {
System.out.println("Binary: " + number);
}
}
} Produces: Even: 2
Even: 4
Binary: 1
Binary: 10
Binary: 11
Binary: 100 Although I am afraid to add this to my project at work. I worry for the maintainer after me. UPDATE: A coworker called me to the mat for extreme terseness. Demonstrating a more fluent style, I refactored the demo code: public class SelectAndProjectMain {
public static void main(final String... args) {
final List<Integer> numbers = asList(1, 2, 3, 4);
for (final Integer number : over(numbers).select(evens()))
System.out.println("Even: " + number);
for (final String number : over(numbers).project(binaries()))
System.out.println("Binary: " + number);
}
static class Evens
implements Cursorable.Select<Integer> {
static Evens evens() {
return new Evens();
}
@Override
public boolean accept(final Integer input) {
return 0 == input % 2;
}
}
static class Binaries
implements Cursorable.Project<Integer, String> {
static Binaries binaries() {
return new Binaries();
}
@Override
public String on(final Integer input) {
return Integer.toBinaryString(input);
}
}
} UPDATE: And lastly, more on Option from the land of Scala.
- Where is a tool for parsing date ranges in Java?
I'd like to turn this 20090701,20090703-20090710 into a list of dates in Java. It's not hard to do, but I hate writing code like this when I am just certain there's already a library for this. Except I can't find one. Any pointers for an open-source library to parse date ranges in Java would be much appreciated. I'm embarrassed to reinvent this whell.
- Shell gotcha: when comments execute
The problem: a colleague has a script which dies early, something like this (with the -x flag): + [ 2 -eq 2 ]
+ break
+ exit The corresponding shell (KSH, but same results in BASH): if [ $cond1 -eq $cond2 ]
then
break
fi
# Lots of commented out code
more commands ... The curiosity: why the early exit? The logic does not have an exit anywhere nearby; the commands following do not call exit. Follow Sherlock Holmes' dictum, How often have I said to you that when you have eliminated the impossible, whatever remains, however improbable, must be the truth? (The Sign of the Four) The only thing left is the commented out code. Sure enough: # Blah, blah
# Something with $(hidden command) buried inside
# More blahbage I had completely forgotten that the shell expands variables even inside comments! In this case, the expanded $(hidden command) blew up and caused the shell to exit prematurely. Caveat plicator.
- A pleasant read: call/cc
Here's a pretty good explanation of call/cc.
- Dataflow concurrency for Java
I ran across this interesting Scala class by Vaclav Pech which makes the data concurrent rather than the code (if I understood the author correctly). The ~ (extraction) and << (insertion) operators looked nifty. Looking to do the same in Java, I see four key requirements: - Insertion can happen only once.
- Extraction is idempotent (infinitely repeatable, non-modifying).
- Extraction blocks until insertion has completed.
- Insertion and extraction are both atomic.
With these in mind, I write this: public class DataFlowReference<T> {
private final ReentrantLock lock = new ReentrantLock();
private final Condition condition = lock.newCondition();
private volatile T item;
private volatile boolean set = false;
private volatile boolean called = false;
public T get()
throws InterruptedException {
lock.lockInterruptibly();
try {
while (true) {
if (set)
return item;
if (!called) {
called = true;
onFirstGet();
continue;
}
condition.await();
}
} finally {
lock.unlock();
}
}
public void setOnce(final T value) {
lock.lock();
try {
if (set)
return;
set = true;
item = value;
condition.signalAll();
} finally {
lock.unlock();
}
}
protected void onFirstGet() {
}
// Object
@Override
public boolean equals(final Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
final DataFlowReference that = (DataFlowReference) o;
return set && item.equals(that.item);
}
@Override
public int hashCode() {
return set ? item.hashCode() : super.hashCode();
}
@Override
public String toString() {
return "(" + set + ':' + item + ')';
}
} Use of DataFlowReference follows the examples from Vaclav's page: declare dataflow references, create threads whose runables use setOnce() and get(), invoke them all together. onFirstGet() supports "just in time" supply of item and would call setOnce(T) with a fresh value. UPDATE: The original version of this class had a horrible, obvious race condition. Caveat plicator.
- Deep understanding of the JVM
Gary Benson posts a deep understanding of the JVM from the team at RedHat porting it to non-x86 architectures.
- HTTPS, blow by blow
Jeff Moser presents a blow-by-blow account of HTTPS. I knew most of this stuff in a hand-waving chalkboard way. Now I feel less ignorant about what actually happens when I connect to a secure web site.
- Braithwaite Correlation
Thanks to Uncle Bob for showing me a new code metric, Braithwaite Correlation originally found by Keith Braithwaite. Pareto distributions, I love it! (Also known as the 80-20 rule.)
P.S. — Oops, wrong Braithwaite.
- Now reading...
Now reading Clojure - getting_started.
- Jumping the work queue in Executor
My server has an ExecutorService for job scheduling. It came up that we need to have some jobs "jump the queue", that is, go to the front of the line waiting for the next free thread. What to do? A web search came up empty. After hemming and hawing, and polling other teams, I went with this solution (below). Comments and improvements welcome. public class PriorityExecutor
extends ThreadPoolExecutor {
public PriorityExecutor() {
super(0, Integer.MAX_VALUE, 60L, SECONDS,
new PriorityBlockingQueue<Runnable>(11,
new PriorityTaskComparator()));
}
public PriorityExecutor(final ThreadFactory threadFactory) {
super(0, Integer.MAX_VALUE, 60L, SECONDS,
new PriorityBlockingQueue<Runnable>(11,
new PriorityTaskComparator()), threadFactory);
}
public PriorityExecutor(final RejectedExecutionHandler handler) {
super(0, Integer.MAX_VALUE, 60L, SECONDS,
new PriorityBlockingQueue<Runnable>(11,
new PriorityTaskComparator()), handler);
}
public PriorityExecutor(final ThreadFactory threadFactory,
final RejectedExecutionHandler handler) {
super(0, Integer.MAX_VALUE, 60L, SECONDS,
new PriorityBlockingQueue<Runnable>(11,
new PriorityTaskComparator()), threadFactory, handler);
}
@Override
protected <T> RunnableFuture<T> newTaskFor(final Callable<T> callable) {
if (callable instanceof Important)
return new PriorityTask<T>(((Important) callable).getPriority(),
callable);
else
return new PriorityTask<T>(0, callable);
}
@Override
protected <T> RunnableFuture<T> newTaskFor(final Runnable runnable,
final T value) {
if (runnable instanceof Important)
return new PriorityTask<T>(((Important) runnable).getPriority(),
runnable, value);
else
return new PriorityTask<T>(0, runnable, value);
}
public interface Important {
int getPriority();
}
private static final class PriorityTask<T>
extends FutureTask<T>
implements Comparable<PriorityTask<T>> {
private final int priority;
public PriorityTask(final int priority, final Callable<T> tCallable) {
super(tCallable);
this.priority = priority;
}
public PriorityTask(final int priority, final Runnable runnable,
final T result) {
super(runnable, result);
this.priority = priority;
}
@Override
public int compareTo(final PriorityTask<T> o) {
final long diff = o.priority - priority;
return 0 == diff ? 0 : 0 > diff ? -1 : 1;
}
}
private static class PriorityTaskComparator
implements Comparator<Runnable> {
@Override
public int compare(final Runnable left, final Runnable right) {
return ((PriorityTask) left).compareTo((PriorityTask) right);
}
}
} Some points: Callers create their own Runnable and Callable classes which implement Important to communicate their priority. Then simply submit instances of these as usual. Jobs with the highest priority jump to the front of the work queue in the executor waiting for the next free thread.
- Fluent assertions, bonus questions
Alex Ruiz tipped me off to an update to FEST, a fluent assertion library. Thanks, Alex! Bonus questions. Everyone know this idiom: <K, V> V get(final K key) {
final V value = concurrentMap.get(key);
if (null != value) return value;
concurrentMap.putIfAbsent(key, factoryMethodForV());
return concurrentMap.get(key);
} Easier question: How do you support valid null values for the concurrent map? (Hint: monads) Harder question: Suppose factoryMethodForV() is expensive. How do you guarantee it is only ever called once for a given key? UPDATE: Fixed some typos.
|
|