Example usage for java.lang Thread start

List of usage examples for java.lang Thread start

Introduction

In this page you can find the example usage for java.lang Thread start.

Prototype

public synchronized void start() 

Source Link

Document

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

Usage

From source file:com.frostwire.gui.library.LibraryUtils.java

public static void asyncAddRadioStation(final String url) {
    Thread t = new Thread(new Runnable() {
        public void run() {
            addRadioStation(url);//from   w ww . ja v a2s . c  o m
        }
    }, "ImportRadioStation");
    t.setDaemon(true);
    t.start();
}

From source file:gda.util.ElogEntry.java

/**
 * Async version of post @see ElogEntry.post
 * /* ww  w  . ja v  a  2  s. c om*/
 * @param title
 * @param content
 * @param userID
 * @param visit
 * @param logID
 * @param groupID
 * @param fileLocations
 */
public static void postAsyn(final String title, final String content, final String userID, final String visit,
        final String logID, final String groupID, final String[] fileLocations) {

    Thread t = uk.ac.gda.util.ThreadManager.getThread(new Runnable() {
        @Override
        public void run() {
            try {
                ElogEntry.post(title, content, userID, visit, logID, groupID, fileLocations);
            } catch (Exception e) {
                Logger logger = LoggerFactory.getLogger(ElogEntry.class);
                logger.error(e.getMessage(), e);
            }
        }
    }, "ElogEntry: " + title);

    t.start();
}

From source file:com.jhash.oimadmin.Utils.java

public static void executeAsyncOperation(String operationName, Runnable operation) {
    logger.debug("Setting up execution of {} in separate thread", operationName);
    Thread oimConnectionThread = threadFactory.newThread(new Runnable() {

        @Override//from w  w w .  j a v a 2 s .  c o  m
        public void run() {
            try {
                logger.debug("Trying to run operation {}", operationName);
                operation.run();
                logger.debug("Completed operation {}.", operationName);
            } catch (Exception exception) {
                logger.warn("Failed to run operation " + operationName, exception);
            }
        }
    });
    oimConnectionThread.setDaemon(true);
    oimConnectionThread.setName(operationName);
    oimConnectionThread.start();
    logger.debug("Completed setup of execution of {} in separate thread", operationName);
}

From source file:Main.java

public static String runScript(String script) {
    String sRet = "";
    try {//from  w  ww  . j  av a  2s.c o  m
        final Process m_process = Runtime.getRuntime().exec(script);
        final StringBuilder sbread = new StringBuilder();
        Thread tout = new Thread(new Runnable() {
            public void run() {
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(m_process.getInputStream()), 8192);
                String ls_1 = null;
                try {
                    while ((ls_1 = bufferedReader.readLine()) != null) {
                        sbread.append(ls_1).append("\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        tout.start();

        final StringBuilder sberr = new StringBuilder();
        Thread terr = new Thread(new Runnable() {
            public void run() {
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(m_process.getErrorStream()), 8192);
                String ls_1 = null;
                try {
                    while ((ls_1 = bufferedReader.readLine()) != null) {
                        sberr.append(ls_1).append("\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        terr.start();

        // int retvalue = m_process.waitFor();
        while (tout.isAlive()) {
            Thread.sleep(50);
        }
        if (terr.isAlive())
            terr.interrupt();
        String stdout = sbread.toString();
        String stderr = sberr.toString();
        sRet = stdout + stderr;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return sRet;
}

From source file:Main.java

ThreadDemo() {

    Thread t = new Thread(this);
    t.start();
    ClassLoader c = t.getContextClassLoader();
    // sets the context ClassLoader for this Thread
    t.setContextClassLoader(c);/* w w w. j  av  a 2  s . c  om*/
    System.out.println("Class = " + c.getClass());
    System.out.println("Parent = " + c.getParent());
}

From source file:SimpleProxyServer.java

/**
 * runs a single-threaded proxy server on
 * the specified local port. It never returns.
 *//*from  ww  w.  ja  va2 s. com*/
public static void runServer(String host, int remoteport, int localport) throws IOException {
    // Create a ServerSocket to listen for connections with
    ServerSocket ss = new ServerSocket(localport);

    final byte[] request = new byte[1024];
    byte[] reply = new byte[4096];

    while (true) {
        Socket client = null, server = null;
        try {
            // Wait for a connection on the local port
            client = ss.accept();

            final InputStream streamFromClient = client.getInputStream();
            final OutputStream streamToClient = client.getOutputStream();

            // Make a connection to the real server.
            // If we cannot connect to the server, send an error to the
            // client, disconnect, and continue waiting for connections.
            try {
                server = new Socket(host, remoteport);
            } catch (IOException e) {
                PrintWriter out = new PrintWriter(streamToClient);
                out.print("Proxy server cannot connect to " + host + ":" + remoteport + ":\n" + e + "\n");
                out.flush();
                client.close();
                continue;
            }

            // Get server streams.
            final InputStream streamFromServer = server.getInputStream();
            final OutputStream streamToServer = server.getOutputStream();

            // a thread to read the client's requests and pass them
            // to the server. A separate thread for asynchronous.
            Thread t = new Thread() {
                public void run() {
                    int bytesRead;
                    try {
                        while ((bytesRead = streamFromClient.read(request)) != -1) {
                            streamToServer.write(request, 0, bytesRead);
                            streamToServer.flush();
                        }
                    } catch (IOException e) {
                    }

                    // the client closed the connection to us, so close our
                    // connection to the server.
                    try {
                        streamToServer.close();
                    } catch (IOException e) {
                    }
                }
            };

            // Start the client-to-server request thread running
            t.start();

            // Read the server's responses
            // and pass them back to the client.
            int bytesRead;
            try {
                while ((bytesRead = streamFromServer.read(reply)) != -1) {
                    streamToClient.write(reply, 0, bytesRead);
                    streamToClient.flush();
                }
            } catch (IOException e) {
            }

            // The server closed its connection to us, so we close our
            // connection to our client.
            streamToClient.close();
        } catch (IOException e) {
            System.err.println(e);
        } finally {
            try {
                if (server != null)
                    server.close();
                if (client != null)
                    client.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:ThreadDemo.java

ThreadDemo(String str) {
    Thread t = new Thread(this, str);
    t.start();
}

From source file:Main.java

public static String runScript(String script) {
    String sRet = "";
    try {//from  w  w w  . j  av  a  2 s. c o  m
        final Process m_process = Runtime.getRuntime().exec(script);
        final StringBuilder sbread = new StringBuilder();
        Thread tout = new Thread(new Runnable() {
            public void run() {
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(m_process.getInputStream()), 8192);
                String ls_1 = null;
                try {
                    while ((ls_1 = bufferedReader.readLine()) != null) {
                        sbread.append(ls_1).append("\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        tout.start();

        final StringBuilder sberr = new StringBuilder();
        Thread terr = new Thread(new Runnable() {
            public void run() {
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(m_process.getErrorStream()), 8192);
                String ls_1 = null;
                try {
                    while ((ls_1 = bufferedReader.readLine()) != null) {
                        sberr.append(ls_1).append("\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        terr.start();

        int retvalue = m_process.waitFor();
        while (tout.isAlive()) {
            Thread.sleep(50);
        }
        if (terr.isAlive())
            terr.interrupt();
        String stdout = sbread.toString();
        String stderr = sberr.toString();
        sRet = stdout + stderr;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return sRet;
}

From source file:examples.IOUtil.java

public static final void readWrite(final InputStream remoteInput, final OutputStream remoteOutput,
        final InputStream localInput, final OutputStream localOutput) {
    Thread reader, writer;

    reader = new Thread() {
        public void run() {
            int ch;

            try {
                while (!interrupted() && (ch = localInput.read()) != -1) {
                    remoteOutput.write(ch);
                    remoteOutput.flush();
                }//from w  w w  .  j  av  a  2 s. c om
            } catch (IOException e) {
                //e.printStackTrace();
            }
        }
    };

    writer = new Thread() {
        public void run() {
            try {
                Util.copyStream(remoteInput, localOutput);
            } catch (IOException e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
    };

    writer.setPriority(Thread.currentThread().getPriority() + 1);

    writer.start();
    reader.setDaemon(true);
    reader.start();

    try {
        writer.join();
        reader.interrupt();
    } catch (InterruptedException e) {
    }
}

From source file:com.llkj.cm.restfull.network.NetworkConnection.java

/**
 * By default the user agent is empty. If you want to use the standard Android user agent, call this method before using the
 * <code>retrieveResponseFromService</code> methods
 * /*w  w w .ja va  2  s. c o  m*/
 * @param context The context
 */
public static void generateDefaultUserAgent(final Context context) {
    if (sDefaultUserAgent != null) {
        return;
    }

    try {
        Constructor<WebSettings> constructor = WebSettings.class.getDeclaredConstructor(Context.class,
                WebView.class);
        constructor.setAccessible(true);
        try {
            WebSettings settings = constructor.newInstance(context, null);
            sDefaultUserAgent = settings.getUserAgentString();
        } finally {
            constructor.setAccessible(false);
        }
    } catch (Exception e) {
        if (Thread.currentThread().getName().equalsIgnoreCase("main")) {
            WebView webview = new WebView(context);
            sDefaultUserAgent = webview.getSettings().getUserAgentString();
        } else {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    Looper.prepare();
                    WebView webview = new WebView(context);
                    sDefaultUserAgent = webview.getSettings().getUserAgentString();
                    Looper.loop();
                }
            };
            thread.start();
        }
    }
}