Example usage for java.lang Runnable run

List of usage examples for java.lang Runnable run

Introduction

In this page you can find the example usage for java.lang Runnable run.

Prototype

public abstract void run();

Source Link

Document

When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread.

Usage

From source file:com.manning.androidhacks.hack023.net.NetworkUtilities.java

public static Thread performOnBackgroundThread(final Runnable runnable) {
    final Thread t = new Thread() {
        @Override/*from  w w w.j ava 2 s .c om*/
        public void run() {
            try {
                runnable.run();
            } finally {

            }
        }
    };
    t.start();
    return t;
}

From source file:Main.java

public static void addGlobalLayoutRequest(final View v, final Runnable runnable) {
    v.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            if (runnable != null)
                runnable.run();

            removeOnGlobalLayoutListener(v, this);
        }/*  www  .  jav a 2 s  . c  om*/
    });

    v.requestLayout();
}

From source file:eu.esdihumboldt.util.http.ProxyUtil.java

private static void init() {
    synchronized (initializers) {
        if (!initialized) {
            for (Runnable initializer : initializers) {
                try {
                    initializer.run();
                } catch (Exception e) {
                    _log.error("Error executing proxy initializer", e); //$NON-NLS-1$
                }/*  ww  w  .  j a  v a 2s  .  co m*/
            }

            initializers.clear();

            initialized = true;
        }
    }
}

From source file:com.textocat.textokit.commons.io.axml.AXMLReader.java

/**
 * Populate the specified CAS by a text and annotations from the specified
 * input assuming that it is formatted as described above.
 *
 * @param in  input Reader. It is a caller's responsibility to close this
 *            reader instance.//from   w  w  w  .j  av a2  s.com
 * @param cas CAS
 * @throws IOException
 * @throws SAXException
 */
public static void read(Reader in, final CAS cas) throws IOException, SAXException {
    XMLReader xmlReader = XMLReaderFactory.createXMLReader();
    AXMLContentHandler contentHandler = new AXMLContentHandler(cas.getTypeSystem());
    xmlReader.setContentHandler(contentHandler);
    InputSource inputSource = new InputSource(in);
    xmlReader.parse(inputSource);
    cas.setDocumentText(contentHandler.getText());
    // from axml ID to CAS FS
    final Map<Annotation, FeatureStructure> mapped = Maps.newHashMap();
    List<Runnable> delayedFeatureAssignments = Lists.newLinkedList();
    //
    for (Annotation _anno : contentHandler.getAnnotations()) {
        String typeName = _anno.getType();
        Type type = cas.getTypeSystem().getType(typeName);
        if (type == null) {
            throw new IllegalStateException(String.format("Unknown type: %s", typeName));
        }
        final AnnotationFS anno = cas.createAnnotation(type, _anno.getBegin(), _anno.getEnd());
        // set primitive features
        for (String featName : _anno.getFeatureNames()) {
            final Feature feat = type.getFeatureByBaseName(featName);
            if (feat == null)
                throw new IllegalStateException(
                        String.format("%s does not have feature %s", type.getName(), featName));
            if (feat.getRange().isPrimitive()) {
                String featValStr = _anno.getFeatureStringValue(featName);
                if (featValStr != null) {
                    anno.setFeatureValueFromString(feat, featValStr);
                }
            } else {
                if (feat.getRange().isArray()) {
                    final List<Annotation> srcFSes = _anno.getFeatureFSArrayValue(featName);
                    delayedFeatureAssignments.add(new Runnable() {
                        @Override
                        public void run() {
                            List<FeatureStructure> mappedFSes = Lists.transform(srcFSes,
                                    new Function<Annotation, FeatureStructure>() {
                                        @Override
                                        public FeatureStructure apply(Annotation srcFS) {
                                            FeatureStructure mappedFS = mapped.get(srcFS);
                                            if (mappedFS == null)
                                                throw new IllegalStateException();
                                            return mappedFS;
                                        }
                                    });
                            anno.setFeatureValue(feat, FSCollectionFactory.createArrayFS(cas, mappedFSes));
                        }
                    });
                } else {
                    final Annotation srcFS = _anno.getFeatureFSValue(featName);
                    delayedFeatureAssignments.add(new Runnable() {
                        @Override
                        public void run() {
                            FeatureStructure mappedFS = mapped.get(srcFS);
                            if (mappedFS == null)
                                throw new IllegalStateException();
                            anno.setFeatureValue(feat, mappedFS);
                        }
                    });
                }
            }
        }
        cas.addFsToIndexes(anno);
        mapped.put(_anno, anno);
    }
    // PHASE II -- set FS and FSArray features
    for (Runnable r : delayedFeatureAssignments) {
        r.run();
    }
}

From source file:Main.java

/**
 * Thread-friendly wrapper method for <code>JComboBox.setSelectedItem</code>.
 * @param component//from   w w w . j  ava 2  s.c o  m
 * @param selectedItem
 *
 * @see javax.swing.JComboBox.setSelectedItem(Object)
 * @deprecated
 */
@Deprecated
public static void setSelectedItem(final JComboBox component, final Object selectedItem) {
    Runnable r = new Runnable() {

        public void run() {
            component.setSelectedItem(selectedItem);
        }
    };

    if (EventQueue.isDispatchThread()) {
        r.run();
        return;
    }

    runTask(r, true);
}

From source file:Main.java

/** Runs a piece of code just before the next draw, after layout and measurement */
public static void doOnPreDraw(final View view, final boolean drawNextFrame, final Runnable runnable) {
    final OnPreDrawListener listener = new OnPreDrawListener() {
        @Override//from  ww w  .  j a va2  s.co m
        public boolean onPreDraw() {
            view.getViewTreeObserver().removeOnPreDrawListener(this);
            runnable.run();
            return drawNextFrame;
        }
    };
    view.getViewTreeObserver().addOnPreDrawListener(listener);
}

From source file:Main.java

public static void runOnLayoutDone(final View view, final Runnable runnable) {
    OnGlobalLayoutListener l = new OnGlobalLayoutListener() {

        @Override// w w  w  .  j a  va 2s  .  co m
        public void onGlobalLayout() {
            view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            runnable.run();
        }
    };
    view.getViewTreeObserver().addOnGlobalLayoutListener(l);
}

From source file:net.landora.video.utils.UIUtils.java

public static void invokeLaterInSwingThread(Runnable r) {
    if (EventQueue.isDispatchThread()) {
        r.run();
    } else {/*from  w  w  w. j  a  v  a 2  s  .c om*/
        SwingUtilities.invokeLater(r);
    }
}

From source file:Main.java

public static void doInAsync(final Runnable runnable) {
    new AsyncTask<Void, Void, Void>() {

        @Override/*  w w  w. j av a  2s  .  c om*/
        protected Void doInBackground(Void... params) {
            return null;
        }

        protected void onPostExecute(Void result) {
            runnable.run();
        }

    }.execute();
}

From source file:net.landora.video.utils.UIUtils.java

public static void invokeInSwingThread(Runnable r) {
    if (EventQueue.isDispatchThread()) {
        r.run();
    } else {/* www  .j av a2s.  com*/
        try {
            SwingUtilities.invokeAndWait(r);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}