Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ANNOUNCE: Submit Java Tasks to Grand Central Dispatch (GCDExecutorService)




On Oct 31, 2009, at 3:30 PM, Mike Swingler wrote:

This could be cheaper if you use JNFThreadDetachOnThreadDeath, because it would allow the GCD dispatch thread to stay attached to the JVM (causing the next JNFObtainEnv() to return quickly). 

This turns out to have some interesting performance implications. I wrote a trivial runnable that does nothing but call System.nanoTime() to measure average elapsed time to get into the Java code.

public class GCDTiming {
    private static final int REPS = 100000;
    private static AtomicLong realReps= new AtomicLong(0);
    private static AtomicLong acc= new AtomicLong(0);

    public static void main(String[] args) throws InterruptedException {
        GCDExecutor exec = new GCDExecutor();
        final long start = System.nanoTime();
        for(int i=0; i < REPS; i++) {
            exec.execute(new Runnable() {
                @Override
                public void run() {
                    acc.getAndAdd((System.nanoTime() - start));
                    realReps.getAndIncrement();
                }
            });
        }
        System.out.println("Elapsed: " + TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS) +
                " avg: " + TimeUnit.MILLISECONDS.convert(acc.get() / realReps.get(), TimeUnit.NANOSECONDS) + " real reps: " + realReps.get());
       // Thread.sleep(5000);
        System.out.println("Elapsed: " + TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS) +
                " avg: " + TimeUnit.MILLISECONDS.convert(acc.get() / realReps.get(), TimeUnit.NANOSECONDS) + " real reps: " + realReps.get());
    }

Where acc is summing up the total time and realReps sanity checking how many times the Runnable really runs.
With JNFThreadDetachOnThreadDeath vs JNFThreadDetachImmediately there is basically an order of magnitude of difference in the elapsed time. i.e. DetachImmediately is 10 times slower or more.

The only downside to DetachOnThreadDeath is the vm takes 5 seconds to shut down consistently... i.e. the last output is printed from the program, but the VM doesn't quit for 5 whole seconds, whereas with DetachImmediately it quits very quickly. This is a bit annoying, but not a major concern in a real application, I feel. So all in all I am inclined to implement it with JNFThreadDetachOnThreadDeath.


AndyT (lordpixel - the cat who walks through walls)
A little bigger on the inside

(see you later space cowboy, you can't take the sky from me)


Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.