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:mosaicsimulation.MosaicLockstepServer.java

public static void main(String[] args) {
    Options opts = new Options();
    opts.addOption("s", "serverPort", true, "Listening TCP port used to initiate handshakes");
    opts.addOption("n", "nClients", true, "Number of clients that will participate in the session");
    opts.addOption("t", "tickrate", true, "Number of transmission session to execute per second");
    opts.addOption("m", "maxUDPPayloadLength", true, "Max number of bytes per UDP packet");
    opts.addOption("c", "connectionTimeout", true, "Timeout for UDP connections");

    DefaultParser parser = new DefaultParser();
    CommandLine commandLine = null;/*from  w  ww  .j a va2s .  c  o m*/
    try {
        commandLine = parser.parse(opts, args);
    } catch (ParseException ex) {
        ex.printStackTrace();
        System.exit(1);
    }

    int serverPort = Integer.parseInt(commandLine.getOptionValue("serverPort"));
    int nClients = Integer.parseInt(commandLine.getOptionValue("nClients"));
    int tickrate = Integer.parseInt(commandLine.getOptionValue("tickrate"));
    int maxUDPPayloadLength = Integer.parseInt(commandLine.getOptionValue("maxUDPPayloadLength"));
    int connectionTimeout = Integer.parseInt(commandLine.getOptionValue("connectionTimeout"));

    Thread serverThread = LockstepServer.builder().clientsNumber(nClients).tcpPort(serverPort)
            .tickrate(tickrate).maxUDPPayloadLength(maxUDPPayloadLength).connectionTimeout(connectionTimeout)
            .build();

    serverThread.setName("Main-server-thread");
    serverThread.start();

    try {
        serverThread.join();
    } catch (InterruptedException ex) {
        LOG.error("Server interrupted while joining");
    }
}

From source file:dm.Interceptors.ElementalReverseProxy.java

public static void main(final String[] args) throws Exception {
    /* if (args.length < 1) {
         System.err.println("Please specified target hostname and port");
         System.exit(1);//from w w w  .j  a v a  2 s.  c o  m
     }
     final String hostname = args[0];
     int port = 80;
     if (args.length > 1) {
         port = Integer.parseInt(args[1]);
     }*/
    final HttpHost target = new HttpHost("127.0.0.1", 80);

    final Thread t = new RequestListenerThread(8888, target);
    t.setDaemon(false);
    t.start();
}

From source file:cn.heroes.ud.protocol.ElementalReverseProxy.java

public static void main(final String[] args) throws Exception {
    if (args.length < 1) {
        System.err.println("Please specified target hostname and port");
        System.exit(1);//from   www  .  j a  va2  s.  c  o  m
    }
    final String hostname = args[0];
    int port = 80;
    if (args.length > 1) {
        port = Integer.parseInt(args[1]);
    }
    final HttpHost target = new HttpHost(hostname, port);

    final Thread t = new RequestListenerThread(8888, target);
    t.setDaemon(false);
    t.start();
}

From source file:com.bfd.job.testClient.t04.ElementalHttpServer.java

public static void main(String[] args) throws Exception {
    /**//from w ww.  ja v  a2 s  .com
     * if (args.length < 1) {
     * System.err.println("Please specify document root directory");
     * System.exit(1); } // Document root directory String docRoot =
     * args[0];
     */
    String docRoot = "c:/root";
    int port = 8080;
    if (args.length >= 2) {
        port = Integer.parseInt(args[1]);
    }

    // Set up the HTTP protocol processor
    HttpProcessor httpproc = HttpProcessorBuilder.create().add(new ResponseDate())
            .add(new ResponseServer("Test/1.1")).add(new ResponseContent()).add(new ResponseConnControl())
            .build();

    // Set up request handlers
    UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper();
    reqistry.register("*", new HttpFileHandler(docRoot));

    // Set up the HTTP service
    HttpService httpService = new HttpService(httpproc, reqistry);

    SSLServerSocketFactory sf = null;
    if (port == 8443) {
        // Initialize SSL context
        ClassLoader cl = ElementalHttpServer.class.getClassLoader();
        URL url = cl.getResource("my.keystore");
        if (url == null) {
            System.out.println("Keystore not found");
            System.exit(1);
        }
        KeyStore keystore = KeyStore.getInstance("jks");
        keystore.load(url.openStream(), "secret".toCharArray());
        KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmfactory.init(keystore, "secret".toCharArray());
        KeyManager[] keymanagers = kmfactory.getKeyManagers();
        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(keymanagers, null, null);
        sf = sslcontext.getServerSocketFactory();
    }

    Thread t = new RequestListenerThread(port, httpService, sf);
    t.setDaemon(false);
    t.start();
}

From source file:Httpd.java

public static void main(String[] args) throws Exception {
    if (args.length < 1) {
        System.err.println("Please specify document root directory");
        System.exit(1);//from w  w  w  .  j a v  a  2 s . co m
    }
    Thread t = new RequestListenerThread(8088, args[0]);
    t.setDaemon(false);
    t.start();
}

From source file:MainServer.java

public static void main(String[] args) {
    int port = 1234;
    String filepath = "";
    String complete_path = "";
    String connection_type = "";
    String ip_address = "";
    int port_out = 0;
    int delay = 20; //20 by default

    //parse commands using getOpt (cli)
    //add Options
    Options options = new Options();

    options.addOption("p", true, "port_to_listen_on");
    options.addOption("d", true, "directory");
    options.addOption("T", false, "TCP mode");
    options.addOption("U", false, "UDP mode");
    options.addOption("s", true, "iPAddress");
    options.addOption("P", true, "port_to_connect_to");
    options.addOption("D", true, "delay");

    CommandLineParser clp = new DefaultParser();
    try {//  w  w w .  j  a  v  a2  s .  co m
        CommandLine cl = clp.parse(options, args);

        //options for the server
        if (cl.hasOption("p")) {
            port = Integer.parseInt(cl.getOptionValue("p"));
        } else {
            System.err.println("No valid port selected.");
            return;
        }

        if (cl.hasOption("d")) {
            filepath = cl.getOptionValue("d");
            //if there a '/' in front, remove it to make it a valid directory
            if (filepath.substring(0, 1).equalsIgnoreCase("/")) {
                filepath = filepath.substring(1);
            }
        } else {
            System.err.println("No valid directory given.");
            return;
        }

        if (cl.hasOption("D")) {
            delay = Integer.parseInt(cl.getOptionValue("D"));
        }

        //options for the client
        if (cl.hasOption("T")) {
            connection_type = "T";
        } else if (cl.hasOption("U")) {
            connection_type = "U";
        }

        if (cl.hasOption("s")) {
            ip_address = cl.getOptionValue("s");
        }

        if (cl.hasOption("P")) {
            port_out = Integer.parseInt(cl.getOptionValue("P"));
        }

    } catch (ParseException e) {
        //TODO: handle exception
    }

    //create directory (if it doesn't already exist)
    try {
        Files.createDirectories(Paths.get(filepath));
    } catch (Exception e) {
        //TODO: handle exception
        System.err.println("Couldn't create directory");
        System.err.println(filepath);
    }

    //read in required files (create them if they dont already exist)
    try {
        Files.createFile(Paths.get(filepath + "/gossip.txt"));
        Files.createFile(Paths.get(filepath + "/peers.txt"));
    } catch (Exception e) {
        //TODO: handle exception
    }
    WriteToFiles.readFiles(filepath);

    //start the servers
    TCPServerSock tcpServer = new TCPServerSock(port, filepath, delay);
    UDPServer udpServer = new UDPServer(port, filepath);

    Thread tcpThread = new Thread(tcpServer);
    Thread udpThread = new Thread(udpServer);

    tcpThread.start();
    udpThread.start();

    //start the client
    if (!connection_type.equals("") && port_out != 0 && !ip_address.equals("")) {
        Client client = new Client(ip_address, port_out, connection_type);
        Thread clientThread = new Thread(client);
        clientThread.start();
    }

    //Start thread to forget peers
    ForgetPeer forgetPeer = new ForgetPeer(filepath + "/peers.txt", delay);
    Thread forgetPeerThread = new Thread(forgetPeer);
    forgetPeerThread.start();
}

From source file:org.atomspace.pi2c.runtime.Server.java

public static void main(String[] args) throws Exception {
    System.err.println("::: ----------------------------------------------------------------------- :::");
    System.err.println("::: ------------------------------  STARTING  ------------------------------:::");
    System.err.println("::: ----------------------------------------------------------------------- :::");
    System.err.println("\n::: SYSTEM-Properties:                                                    :::");
    Set<?> properties = System.getProperties().keySet();
    for (Object object : properties) {
        System.err.println("::: " + object.toString() + " = " + System.getProperty(object.toString()));
    }//from  w w  w. j  av a  2  s  .  c  om
    System.err.println("\n::: ENV-Properties:                                                       :::");
    properties = System.getenv().keySet();
    for (Object object : properties) {
        System.err.println("::: " + object.toString() + " = " + System.getenv(object.toString()));
    }

    windows = System.getProperty("os.name").toLowerCase().startsWith("windows");
    linux = System.getProperty("os.name").toLowerCase().startsWith("linux");
    sunos = System.getProperty("os.name").toLowerCase().startsWith("sun");
    freebsd = System.getProperty("os.name").toLowerCase().startsWith("freebsd");

    if (linux || sunos) {
        //USR2-Signal-Handel lookup internal Server STATUS for Linux or SunOS
        System.err.println("::: " + new Date() + "::: run unter Linux or SunOS :::");
        addUnixSignalStatusHandle();
    } else if (freebsd) {
        System.err.println("::: " + new Date() + "::: run unter FreeBSD :::");
    } else if (windows) {
        //Gracefull Shutdown JFrame for Windows, because can not kill -15 <pid> on Window or in Eclipse Console
        System.err.println("::: " + new Date() + " ::: run unter windows :::");
        addWindowsShutdownHandle();
    } else {
        System.err.println("UNKNOWN OS:" + System.getProperty("os.name"));
    }

    status = STATUS_STARTING;

    Server server = new Server();
    Thread serverThread = new Thread(server);
    serverThread.start();

    //Thread can stop by Shutdown-Hook
    while (true) {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            break;
        }
    }

}

From source file:httpserver.ElementalHttpServer.java

public static void main(String[] args) throws Exception {

    // Clay code, adding arguments to simulate command line execution
    args = new String[2];
    args[0] = "C://Users/Clay/Documents";
    args[1] = "80";

    if (args.length < 1) {
        System.err.println("Please specify document root directory");
        System.exit(1);//from www .ja  va  2  s  .  c  om
    }
    // Document root directory
    String docRoot = args[0];

    // Setting up port, if port was specified, then use that one
    int port = 8080;
    if (args.length >= 2) {
        port = Integer.parseInt(args[1]);
    }

    // Set up the HTTP protocol processor
    HttpProcessor httpproc = HttpProcessorBuilder.create().add(new ResponseDate())
            .add(new ResponseServer("Test/1.1")).add(new ResponseContent()).add(new ResponseConnControl())
            .build();

    // Set up request handlers
    UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper();
    reqistry.register("*", new HttpFileHandler(docRoot));

    // Set up the HTTP service
    HttpService httpService = new HttpService(httpproc, reqistry);

    SSLServerSocketFactory sf = null;
    if (port == 8443) {
        // Initialize SSL context
        ClassLoader cl = ElementalHttpServer.class.getClassLoader();
        URL url = cl.getResource("my.keystore");
        if (url == null) {
            System.out.println("Keystore not found");
            System.exit(1);
        }
        KeyStore keystore = KeyStore.getInstance("jks");
        keystore.load(url.openStream(), "secret".toCharArray());
        KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmfactory.init(keystore, "secret".toCharArray());
        KeyManager[] keymanagers = kmfactory.getKeyManagers();
        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(keymanagers, null, null);
        sf = sslcontext.getServerSocketFactory();
    }

    Thread t = new RequestListenerThread(port, httpService, sf);
    t.setDaemon(false);
    t.start();
}

From source file:io.aos.protocol.http.httpcommon.ElementalReverseProxy.java

public static void main(String... args) throws Exception {
    if (args.length < 1) {
        System.err.println("Please specified target hostname and port");
        System.exit(1);/*from w  w w .j  a va  2s  .c  o  m*/
    }
    String hostname = args[0];
    int port = 80;
    if (args.length > 1) {
        port = Integer.parseInt(args[1]);
    }
    HttpHost target = new HttpHost(hostname, port);

    Thread t = new RequestListenerThread(8888, target);
    t.setDaemon(false);
    t.start();
}

From source file:Main.java

public static void main(String[] args) {
    Thread thread = new Thread() {
        public void run() {
            throw new NullPointerException();
        }/*from w  ww  .j ava  2  s.  c  o  m*/
    };
    thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread arg0, Throwable arg1) {
            arg1.printStackTrace();
        }
    });
    thread.start();
}