Example usage for java.lang Thread Thread

List of usage examples for java.lang Thread Thread

Introduction

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

Prototype

public Thread(Runnable target, String name) 

Source Link

Document

Allocates a new Thread object.

Usage

From source file:org.sourcepit.consul.forwarder.Main.java

public static void main(String[] args) {

    final String dockerUri = "http://192.168.56.101:2375";
    final String consulUri = "http://192.168.56.101:8500";

    final int fetchConsulStateInterval = 30;
    final int fetchDockerStateInterval = 30;
    final int dispatchItemsInterval = 2;
    final int requestDockerStateDelay = 5;

    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
    connManager.setDefaultMaxPerRoute(10);
    final HttpClient httpClient = HttpClients.createMinimal(connManager);

    final BlockingQueue<JsonObject> queue = new LinkedBlockingQueue<>();

    final FetchConsulStateCmd fetchConsulStateCmd = new FetchConsulStateCmd(httpClient, consulUri) {
        @Override//from   ww  w.j  a va  2s.  co  m
        protected void doHandle(JsonObject consulState) {
            final JsonObject item = createItem("ConsulState", consulState);
            LOG.debug(item.toString());
            queue.add(item);
        }

    };

    final FetchDockerStateCmd fetchDockerStateCmd = new FetchDockerStateCmd(httpClient, dockerUri) {
        @Override
        protected void doHandle(JsonArray dockerState) {
            final JsonObject item = createItem("DockerState", dockerState);
            LOG.debug(item.toString());
            queue.add(item);
        }
    };

    final ConsulForwarderState forwarderState = new ConsulForwarderState();

    final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    scheduler.scheduleAtFixedRate(fetchConsulStateCmd, 0, fetchConsulStateInterval, TimeUnit.SECONDS);
    scheduler.scheduleAtFixedRate(fetchDockerStateCmd, 0, fetchDockerStateInterval, TimeUnit.SECONDS);
    scheduler.scheduleAtFixedRate(new DispatchItemsCmd(queue) {
        @Override
        protected void handleDockerState(JsonArray dockerState) {
            forwarderState.applyDockerState(dockerState);
        }

        @Override
        protected void handleDockerEvents(JsonArray dockerEvents) {
            // trigger docker state update
            scheduler.schedule(fetchDockerStateCmd, requestDockerStateDelay, TimeUnit.SECONDS);
        }

        @Override
        protected void handleConsulState(JsonObject consulState) {
            forwarderState.applyConsulState(consulState);
        }
    }, 0, dispatchItemsInterval, TimeUnit.SECONDS);

    final DockerEventObserver eventObserver = new DockerEventObserver(httpClient, dockerUri) {
        @Override
        protected void handle(JsonObject event) {
            queue.add(createItem("DockerEvent", event));
        }
    };

    final Thread eventObserverThread = new Thread(eventObserver, "Docker Event Observer") {
        @Override
        public void interrupt() {
            eventObserver.die();
            super.interrupt();
        }
    };
    eventObserverThread.start();
}

From source file:Supplier.java

public static void main(String[] args) {
    String url = "tcp://localhost:61616";
    String user = null;//  ww  w . ja v a2 s .c o m
    String password = null;
    String item = "HardDrive";

    if (args.length >= 1) {
        item = args[0];
    }
    String queue;
    if ("HardDrive".equals(item)) {
        queue = "StorageOrderQueue";
    } else if ("Monitor".equals(item)) {
        queue = "MonitorOrderQueue";
    } else {
        throw new IllegalArgumentException("Item must be either HardDrive or Monitor");
    }

    if (args.length >= 2) {
        url = args[1];
    }

    if (args.length >= 3) {
        user = args[2];
    }

    if (args.length >= 4) {
        password = args[3];
    }

    Supplier s = new Supplier(item, queue, url, user, password);

    new Thread(s, "Supplier " + item).start();
}

From source file:j8583.example.Server.java

public static void main(String[] args) throws Exception {
    mfact = ConfigParser.createFromClasspathConfig("j8583/example/config.xml");
    System.err.println("Setting up server socket...");
    ServerSocket server = new ServerSocket(9999);
    System.err.println("Waiting for connections...");
    while (true) {
        Socket sock = server.accept();
        System.err.println(String.format("New connection from %s:%d", sock.getInetAddress(), sock.getPort()));
        new Thread(new Server(sock), "j8583-server").start();
    }//from  www.  j  a  v a  2 s. c o m
}

From source file:j8583.example.Client.java

public static void main(String[] args) throws Exception {
    Random rng = new Random(System.currentTimeMillis());
    log.debug("Reading config");
    mfact = ConfigParser.createFromClasspathConfig("j8583/example/config.xml");
    mfact.setAssignDate(true);// w  w w.j av  a2  s .c  o m
    mfact.setTraceNumberGenerator(new SimpleTraceGenerator((int) (System.currentTimeMillis() % 10000)));
    System.err.println("Connecting to server");
    Socket sock = new Socket("localhost", 9999);
    // Send 10 messages, then wait for the responses
    Client client = new Client(sock);
    Thread reader = new Thread(client, "j8583-client");
    reader.start();
    for (int i = 0; i < 10; i++) {
        IsoMessage req = mfact.newMessage(0x200);
        req.setValue(4, amounts[rng.nextInt(amounts.length)], IsoType.AMOUNT, 0);
        req.setValue(12, req.getObjectValue(7), IsoType.TIME, 0);
        req.setValue(13, req.getObjectValue(7), IsoType.DATE4, 0);
        req.setValue(15, req.getObjectValue(7), IsoType.DATE4, 0);
        req.setValue(17, req.getObjectValue(7), IsoType.DATE4, 0);
        req.setValue(37, System.currentTimeMillis() % 1000000, IsoType.NUMERIC, 12);
        req.setValue(41, data[rng.nextInt(data.length)], IsoType.ALPHA, 16);
        req.setValue(48, data[rng.nextInt(data.length)], IsoType.LLLVAR, 0);
        pending.put(req.getField(11).toString(), req);
        System.err.println(String.format("Sending request %s", req.getField(11)));
        req.write(sock.getOutputStream(), 2);
    }
    log.debug("Waiting for responses");
    while (pending.size() > 0 && sock.isConnected()) {
        Thread.sleep(500);
    }
    client.stop();
    reader.interrupt();
    log.debug("DONE.");
}

From source file:com.cloudera.beeswax.Server.java

public static void main(String[] args) throws TTransportException, MetaException, ParseException {
    parseArgs(args);//from  w w  w .  j a  v a  2 s.c o  m
    createDirectoriesAsNecessary();

    // Start metastore if specified
    if (mport != -1) {
        LOG.info("Starting metastore at port " + mport);
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    serveMeta(mport);
                } catch (TTransportException e) {
                    e.printStackTrace();
                } catch (MetaException e) {
                    e.printStackTrace();
                }
            }
        }, "MetaServerThread");
        t.setDaemon(true);
        t.start();
    }

    // Serve beeswax out of the main thread.
    LOG.info("Starting beeswaxd at port " + bport);
    serveBeeswax(bport);
}

From source file:ObjectFIFOTest.java

public static void main(String[] args) {
    final ObjectFIFO fifo = new ObjectFIFO(5);

    Runnable fullCheckRunnable = new Runnable() {
        public void run() {
            fullCheck(fifo);//from   w  w  w  . j  a  v a  2 s. com
        }
    };

    Thread fullCheckThread = new Thread(fullCheckRunnable, "fchk");
    fullCheckThread.setPriority(9);
    fullCheckThread.setDaemon(true); // die automatically
    fullCheckThread.start();

    Runnable emptyCheckRunnable = new Runnable() {
        public void run() {
            emptyCheck(fifo);
        }
    };

    Thread emptyCheckThread = new Thread(emptyCheckRunnable, "echk");
    emptyCheckThread.setPriority(8);
    emptyCheckThread.setDaemon(true); // die automatically
    emptyCheckThread.start();

    Runnable consumerRunnable = new Runnable() {
        public void run() {
            consumer(fifo);
        }
    };

    Thread consumerThread = new Thread(consumerRunnable, "cons");
    consumerThread.setPriority(7);
    consumerThread.start();

    Runnable producerRunnable = new Runnable() {
        public void run() {
            producer(fifo);
        }
    };

    Thread producerThread = new Thread(producerRunnable, "prod");
    producerThread.setPriority(6);
    producerThread.start();
}

From source file:Main.java

public static Thread create(String name, Runnable runnable) {
    return new Thread(runnable, name);
}

From source file:Main.java

public static Thread startThread(String name, Runnable runnable) {
    Thread thread = new Thread(runnable, name);
    thread.start();/*from   w ww .j a  v a 2 s.c om*/
    return thread;
}

From source file:Main.java

public static Thread startNewThread(String name, Runnable runnable) {
    Thread thread = new Thread(runnable, name);
    thread.start();//  www .j a  va2 s  .  c om
    return thread;
}

From source file:Main.java

public static void runLater(final Runnable r) {
    Thread t = new Thread(r, "ThreadUtils.runLater-Thread");
    t.setDaemon(true);/*from  w  w w. ja  v  a 2  s.  c  om*/
    t.start();
}