Example usage for java.lang Object wait

List of usage examples for java.lang Object wait

Introduction

In this page you can find the example usage for java.lang Object wait.

Prototype

public final void wait() throws InterruptedException 

Source Link

Document

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Usage

From source file:Main.java

public static void doWait(Object syncObj) {
    synchronized (syncObj) {
        try {/*from ww  w .  java 2 s  .c  o  m*/
            syncObj.wait();
        } catch (InterruptedException e) {
            Log.d("", "", e);
        }
    }
}

From source file:Main.java

/**
 * Wait on the given object indefinitely. This method will return false
 * if an error occured while waiting. The current thread must hold the 
 * lock on the given object. //  w  w  w .j a  va2s. c o  m
 */
public static boolean wait(Object o) {
    if (Thread.holdsLock(o)) {
        try {
            o.wait();
            return true;
        } catch (InterruptedException e) {
            return false;
        }
    }
    return false;
}

From source file:Main.java

public static void wait(Object monitor) {
    try {//from   www .  j a  va2 s .  c o  m
        // Assumes that the monitor is already "synchronized"
        monitor.wait();
    } catch (InterruptedException ex) {
        // suppress
    }
}

From source file:org.whispersystems.textsecuregcm.util.Util.java

public static void wait(Object object) {
    try {/*  ww w.  j a  va  2 s. co m*/
        object.wait();
    } catch (InterruptedException e) {
        throw new AssertionError(e);
    }
}

From source file:osh.comdriver.simulation.cruisecontrol.ScheduleDrawer.java

public static void showScheduleAndWait(List<Schedule> schedules, HashMap<VirtualCommodity, PriceSignal> ps,
        HashMap<VirtualCommodity, PowerLimitSignal> pls, long currentTime) {

    final Object sync = new Object();

    ScheduleDrawer demo = new ScheduleDrawer(schedules, ps, pls, currentTime);
    JFrame frame = new JFrame("Schedule");
    frame.setContentPane(demo);/*from w w w. ja  v a2 s .co  m*/
    frame.pack();
    RefineryUtilities.centerFrameOnScreen(frame);
    frame.setVisible(true);

    synchronized (sync) {
        try {
            sync.wait();
        } catch (InterruptedException e) {
        }
    }

    frame.dispose();
}

From source file:ImageUtilities.java

/**
 * Starts loading the given image, returns only when it's done loading.
 * Note that it's much more efficient to preload a lot of images at once using
 * the preload(Image []) method instead of this one.
 *
 * @return The result of the image loading, either {@link #COMPLETE},
 * {@link #ERRORED}, {@link #ABORTED} or {@link #INTERRUPTED}.
 *
 * @see #preload(java.awt.Image [], int [])
 *///from  www  .java 2 s  .co m

public static int preload(Image image) {
    Toolkit toolkit = Toolkit.getDefaultToolkit();

    // Check if already loaded
    if ((toolkit.checkImage(image, -1, -1, null) & ImageObserver.ALLBITS) != 0)
        return COMPLETE;

    Object lock = new Object();
    synchronized (lock) {
        while (true) {
            ImageLoadObserver observer = new ImageLoadObserver(lock);
            toolkit.prepareImage(image, -1, -1, observer);
            int result = toolkit.checkImage(image, -1, -1, null);
            if ((result & ImageObserver.ALLBITS) != 0)
                return COMPLETE;
            if ((result & ImageObserver.ERROR) != 0)
                return ERRORED;
            if ((result & ImageObserver.ABORT) != 0)
                return ABORTED;

            try {
                lock.wait();
                return observer.getResult();
            } catch (InterruptedException e) {
                return INTERRUPTED;
            }
        }
    }
}

From source file:com.netflix.iep.http.RxHttpTest.java

@BeforeClass
public static void startServer() throws Exception {
    rxHttp.start();//from ww  w.  ja  v  a2  s .co  m

    server = HttpServer.create(new InetSocketAddress(0), 100);
    server.setExecutor(Executors.newFixedThreadPool(10, new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "HttpServer");
        }
    }));
    port = server.getAddress().getPort();

    server.createContext("/empty", new HttpHandler() {
        @Override
        public void handle(HttpExchange exchange) throws IOException {
            ignore(exchange.getRequestBody());
            int port = exchange.getRemoteAddress().getPort();
            exchange.getResponseHeaders().add("X-Test-Port", "" + port);
            statusCounts.incrementAndGet(statusCode.get());
            exchange.sendResponseHeaders(statusCode.get(), -1L);
            exchange.close();
        }
    });

    server.createContext("/echo", new HttpHandler() {
        @Override
        public void handle(HttpExchange exchange) throws IOException {
            Headers headers = exchange.getRequestHeaders();
            int contentLength = Integer.parseInt(headers.getFirst("Content-Length"));
            String contentEnc = headers.getFirst("Content-Encoding");
            if (contentEnc != null) {
                exchange.getResponseHeaders().add("Content-Encoding", contentEnc);
            }

            int code = statusCode.get();
            if (contentLength > 512 && !"gzip".equals(contentEnc)) {
                code = 400;
            }

            statusCounts.incrementAndGet(code);
            exchange.sendResponseHeaders(code, contentLength);
            try (InputStream input = exchange.getRequestBody();
                    OutputStream output = exchange.getResponseBody()) {
                byte[] buf = new byte[1024];
                int length;
                while ((length = input.read(buf)) > 0) {
                    output.write(buf, 0, length);
                }
            }
            exchange.close();
        }
    });

    server.createContext("/relativeRedirect", new HttpHandler() {
        @Override
        public void handle(HttpExchange exchange) throws IOException {
            ignore(exchange.getRequestBody());
            if (redirects.get() <= 0) {
                statusCounts.incrementAndGet(statusCode.get());
                exchange.getResponseHeaders().add("Location", "/empty");
                exchange.sendResponseHeaders(statusCode.get(), -1L);
                exchange.close();
            } else {
                redirects.decrementAndGet();
                statusCounts.incrementAndGet(302);
                exchange.getResponseHeaders().add("Location", "/relativeRedirect");
                exchange.sendResponseHeaders(302, -1L);
                exchange.close();
            }
        }
    });

    server.createContext("/absoluteRedirect", new HttpHandler() {
        @Override
        public void handle(HttpExchange exchange) throws IOException {
            String host = "http://" + exchange.getRequestHeaders().getFirst("Host");
            ignore(exchange.getRequestBody());
            if (redirects.get() <= 0) {
                statusCounts.incrementAndGet(302);
                exchange.getResponseHeaders().add("Location", host + "/empty");
                exchange.sendResponseHeaders(302, -1L);
                exchange.close();
            } else {
                redirects.decrementAndGet();
                statusCounts.incrementAndGet(302);
                exchange.getResponseHeaders().add("Location", host + "/absoluteRedirect");
                exchange.sendResponseHeaders(302, -1L);
                exchange.close();
            }
        }
    });

    server.createContext("/notModified", new HttpHandler() {
        @Override
        public void handle(HttpExchange exchange) throws IOException {
            ignore(exchange.getRequestBody());
            statusCounts.incrementAndGet(304);
            exchange.sendResponseHeaders(304, -1L);
            exchange.close();
        }
    });

    server.createContext("/redirectNoLocation", new HttpHandler() {
        @Override
        public void handle(HttpExchange exchange) throws IOException {
            ignore(exchange.getRequestBody());
            statusCounts.incrementAndGet(302);
            exchange.sendResponseHeaders(302, -1L);
            exchange.close();
        }
    });

    server.createContext("/readTimeout", new HttpHandler() {
        @Override
        public void handle(HttpExchange exchange) throws IOException {
            ignore(exchange.getRequestBody());
            statusCounts.incrementAndGet(statusCode.get()); // So we can track retries
            Object lock = new Object();
            try {
                synchronized (lock) {
                    lock.wait();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });

    server.start();

    set(client + ".niws.client.MaxAutoRetriesNextServer", "" + retries);
    set(client + ".niws.client.RetryDelay", "100");
    set(client + ".niws.client.ReadTimeout", "1000");
}

From source file:Main.java

public static void streamMidiSequence(URL url)
        throws IOException, InvalidMidiDataException, MidiUnavailableException {
    Sequencer sequencer = null; // Converts a Sequence to MIDI events
    Synthesizer synthesizer = null; // Plays notes in response to MIDI events

    try {/*from  w w  w.java2 s .c o m*/
        // Create, open, and connect a Sequencer and Synthesizer
        // They are closed in the finally block at the end of this method.
        sequencer = MidiSystem.getSequencer();
        sequencer.open();
        synthesizer = MidiSystem.getSynthesizer();
        synthesizer.open();
        sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());

        // Specify the InputStream to stream the sequence from
        sequencer.setSequence(url.openStream());

        // This is an arbitrary object used with wait and notify to
        // prevent the method from returning before the music finishes
        final Object lock = new Object();

        // Register a listener to make the method exit when the stream is
        // done. See Object.wait() and Object.notify()
        sequencer.addMetaEventListener(new MetaEventListener() {
            public void meta(MetaMessage e) {
                if (e.getType() == END_OF_TRACK) {
                    synchronized (lock) {
                        lock.notify();
                    }
                }
            }
        });

        // Start playing the music
        sequencer.start();

        // Now block until the listener above notifies us that we're done.
        synchronized (lock) {
            while (sequencer.isRunning()) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                }
            }
        }
    } finally {
        // Always relinquish the sequencer, so others can use it.
        if (sequencer != null)
            sequencer.close();
        if (synthesizer != null)
            synthesizer.close();
    }
}

From source file:com.hm.SSI.dubbo.DubboProvider.java

@Test
public void testDubboProvider() throws Exception {
    System.in.read();// ww  w  .j a v  a2s.c  o m
    try {
        //new ClassPathXmlApplicationContext("application-RmiService.xml");  
        Object lock = new Object();
        synchronized (lock) {
            lock.wait();
        }
    } catch (BeansException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.dumontierlab.pdb2rdf.Pdb2Rdf.java

private static ExecutorService getThreadPool(CommandLine cmd) {
    // twice the number of PU
    final Object monitor = new Object();
    int numberOfThreads = getNumberOfThreads(cmd);
    LOG.info("Using " + numberOfThreads + " threads.");
    ThreadPoolExecutor threadPool = new ThreadPoolExecutor(numberOfThreads, numberOfThreads, 10,
            TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(1), new RejectedExecutionHandler() {
                @Override// w ww  .  j  a  v a  2s .  c o  m
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    synchronized (monitor) {
                        try {
                            monitor.wait();
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        }
                    }
                    executor.execute(r);
                }
            }) {
        @Override
        protected void afterExecute(Runnable r, Throwable t) {
            synchronized (monitor) {
                monitor.notify();
            }
            super.afterExecute(r, t);
        }
    };

    return threadPool;
}