changeset 88:18abc7d66d49

Async-layer uses daemon thread. Added arg to set the thread priority.
author Mikhail Kryshen <mikhail@kryshen.net>
date Wed, 06 Oct 2010 18:45:51 +0400
parents beb89bd18839
children 54f6e6d196c3
files src/net/kryshen/indyvon/async.clj src/net/kryshen/indyvon/layers.clj
diffstat 2 files changed, 28 insertions(+), 10 deletions(-) [+]
line diff
     1.1 --- a/src/net/kryshen/indyvon/async.clj	Wed Oct 06 18:09:37 2010 +0400
     1.2 +++ b/src/net/kryshen/indyvon/async.clj	Wed Oct 06 18:45:51 2010 +0400
     1.3 @@ -25,7 +25,7 @@
     1.4     (net.kryshen.indyvon.core Size Location)
     1.5     (java.awt Image)
     1.6     (java.awt.image BufferedImage)
     1.7 -   (java.util.concurrent ThreadPoolExecutor
     1.8 +   (java.util.concurrent ThreadFactory ThreadPoolExecutor
     1.9                           ThreadPoolExecutor$DiscardOldestPolicy
    1.10                           ArrayBlockingQueue TimeUnit)))
    1.11  
    1.12 @@ -139,14 +139,30 @@
    1.13    (layer-size [layer]
    1.14      (Size. width height)))
    1.15  
    1.16 +(defn- create-thread-factory [priority]
    1.17 +  (reify
    1.18 +   ThreadFactory
    1.19 +   (newThread [_ runnable]
    1.20 +     (let [thread (Thread. runnable)]
    1.21 +       (when priority
    1.22 +         (.setPriority thread priority))
    1.23 +       (.setDaemon thread true)
    1.24 +       thread))))
    1.25 +
    1.26 +(defn- create-executor [priority]
    1.27 +  (doto (ThreadPoolExecutor.
    1.28 +         (int 1) (int 1)
    1.29 +         (long 0) TimeUnit/SECONDS
    1.30 +         (ArrayBlockingQueue. 1)
    1.31 +         (ThreadPoolExecutor$DiscardOldestPolicy.))
    1.32 +    (.setThreadFactory (create-thread-factory priority))))
    1.33 +
    1.34  (defn async-layer 
    1.35    "Creates layer that draws the content asynchronously using
    1.36     offscreen buffer."
    1.37 -  [content width height]
    1.38 -  (AsyncLayer. content width height
    1.39 -               (ThreadPoolExecutor.
    1.40 -                (int 1) (int 1)
    1.41 -                (long 0) TimeUnit/SECONDS
    1.42 -                (ArrayBlockingQueue. 1)
    1.43 -                (ThreadPoolExecutor$DiscardOldestPolicy.))
    1.44 -               (ref nil)))
    1.45 +  ([content width height]
    1.46 +     (async-layer content width height nil))
    1.47 +  ([content width height priority]
    1.48 +     (AsyncLayer. content width height
    1.49 +                  (create-executor priority)
    1.50 +                  (ref nil))))
     2.1 --- a/src/net/kryshen/indyvon/layers.clj	Wed Oct 06 18:09:37 2010 +0400
     2.2 +++ b/src/net/kryshen/indyvon/layers.clj	Wed Oct 06 18:45:51 2010 +0400
     2.3 @@ -202,6 +202,8 @@
     2.4               height (if (pos? height) height 1)]
     2.5           (Size. width height))))))
     2.6  
     2.7 +(def *miniature-thread-priority* 2)
     2.8 +
     2.9  (defn miniature
    2.10    "Creates layer that asynchronously renders view of the content
    2.11    scaled to the specified size."
    2.12 @@ -217,7 +219,7 @@
    2.13          (draw! content 0 0 (:width size) (:height size))))
    2.14      (layer-size [this]
    2.15        (Size. width height)))
    2.16 -   width height))
    2.17 +   width height *miniature-thread-priority*))
    2.18  
    2.19  (defrecord Viewport [content h-align v-align
    2.20                       ;; State (refs)