RunnableWrapper.java
上传用户:haobig99
上传日期:2022-06-15
资源大小:369k
文件大小:5k
源码类别:

J2ME

开发平台:

Java

  1. /*
  2.  * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
  3.  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4.  *
  5.  * This code is free software; you can redistribute it and/or modify it
  6.  * under the terms of the GNU General Public License version 2 only, as
  7.  * published by the Free Software Foundation.  Sun designates this
  8.  * particular file as subject to the "Classpath" exception as provided
  9.  * by Sun in the LICENSE file that accompanied this code.
  10.  *
  11.  * This code is distributed in the hope that it will be useful, but WITHOUT
  12.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14.  * version 2 for more details (a copy is included in the LICENSE file that
  15.  * accompanied this code).
  16.  *
  17.  * You should have received a copy of the GNU General Public License version
  18.  * 2 along with this work; if not, write to the Free Software Foundation,
  19.  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20.  *
  21.  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22.  * CA 95054 USA or visit www.sun.com if you need additional information or
  23.  * have any questions.
  24.  */
  25. package com.sun.lwuit;
  26. import java.util.Vector;
  27. /**
  28.  * Class used by callSeriallyAndWait and invokeAndBlock and form to save code size
  29.  * 
  30.  * @author Shai Almog
  31.  */
  32. class RunnableWrapper implements Runnable {
  33.     private static final Object THREADPOOL_LOCK = new Object();
  34.     private static Vector threadPool = new Vector();
  35.     private static int threadCount = 0;
  36.     private static int maxThreadCount = 3;
  37.     private static int availableThreads = 0;
  38.     private boolean done = false;
  39.     private Runnable internal;
  40.     private int type;
  41.     private RuntimeException err;
  42.     private Form parentForm;
  43.     private Painter paint;
  44.     private boolean reverse;
  45.     public RunnableWrapper(Form parentForm, Painter paint, boolean reverse) {
  46.         this.parentForm = parentForm;
  47.         this.paint = paint;
  48.         this.reverse = reverse;
  49.     }
  50.     
  51.     public RunnableWrapper(Runnable internal, int type) {
  52.         this.internal = internal;
  53.         this.type = type;
  54.     }
  55.     public RuntimeException getErr() {
  56.         return err;
  57.     }
  58.     public void setDone(boolean done) {
  59.         this.done = done;
  60.     }
  61.     
  62.     public boolean isDone() {
  63.         return done;
  64.     }
  65.     public void run() {
  66.         if(parentForm != null) {
  67.             // set current form uses this portion to make sure all set current operations
  68.             // occur on the EDT
  69.             if(paint == null) {
  70.                 Display.getInstance().setCurrent(parentForm, reverse);
  71.                 return;
  72.             }
  73.             
  74.             Dialog dlg = (Dialog)parentForm;
  75.             while (!dlg.isDisposed()) {
  76.                 try {
  77.                     synchronized(Display.lock) {
  78.                         Display.lock.wait(40);
  79.                     }
  80.                 } catch (InterruptedException ex) {
  81.                 }
  82.             }
  83.             parentForm.getStyle().setBgPainter(paint);
  84.         } else {
  85.             switch(type) {
  86.                 case 0: 
  87.                     internal.run();
  88.                     done = true;
  89.                     synchronized(Display.lock) {
  90.                         Display.lock.notify();
  91.                     }
  92.                     break;
  93.                 case 1:
  94.                     try {
  95.                         internal.run();
  96.                     } catch(RuntimeException ex) {
  97.                         this.err = ex;
  98.                     }
  99.                     break;
  100.                 case 2:
  101.                     while(!done) {
  102.                         synchronized(Display.lock) {
  103.                             try {
  104.                                 Display.lock.wait(10);
  105.                             } catch (InterruptedException ex) {
  106.                                 ex.printStackTrace();
  107.                             }
  108.                         }
  109.                     }
  110.                     break;
  111.                 case 3:
  112.                     Display.getInstance().mainEDTLoop();
  113.                     break;
  114.                 case 4:
  115.                     while(true) {
  116.                         Runnable r = null;
  117.                         synchronized(THREADPOOL_LOCK) {
  118.                             if(threadPool.size() > 0) {
  119.                                 r = (Runnable)threadPool.elementAt(0);
  120.                                 threadPool.removeElementAt(0);
  121.                             } else {
  122.                                 try {
  123.                                     availableThreads++;
  124.                                     THREADPOOL_LOCK.wait();
  125.                                 } catch (InterruptedException ex) {
  126.                                     ex.printStackTrace();
  127.                                 }
  128.                             }
  129.                         }
  130.                         if(r != null) {
  131.                             availableThreads--;
  132.                             r.run();
  133.                         }
  134.                     }
  135.             }
  136.         }
  137.         done = true;
  138.     }
  139.     static void pushToThreadPull(Runnable r) {
  140.         if(availableThreads == 0 && threadCount < maxThreadCount) {
  141.             threadCount++;
  142.             Thread poolThread = new Thread(new RunnableWrapper(null, 4), "invokeAndBlock" + threadCount);
  143.             poolThread.start();
  144.         }
  145.         synchronized(THREADPOOL_LOCK) {
  146.             threadPool.addElement(r);
  147.             THREADPOOL_LOCK.notify();
  148.         }
  149.     }
  150. }