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() 

Source Link

Document

Allocates a new Thread object.

Usage

From source file:org.eclipse.swt.snippets.Snippet151.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 151");
    shell.setLayout(new FillLayout());
    final Table table = new Table(shell, SWT.BORDER | SWT.VIRTUAL);
    table.addListener(SWT.SetData, e -> {
        TableItem item = (TableItem) e.item;
        int index = table.indexOf(item);
        item.setText("Item " + data[index]);
    });/*  w  w  w.  j a va 2s . co m*/
    Thread thread = new Thread() {
        @Override
        public void run() {
            int count = 0;
            Random random = new Random();
            while (count++ < 500) {
                if (table.isDisposed())
                    return;
                // add 10 random numbers to array and sort
                int grow = 10;
                int[] newData = new int[data.length + grow];
                System.arraycopy(data, 0, newData, 0, data.length);
                int index = data.length;
                data = newData;
                for (int j = 0; j < grow; j++) {
                    data[index++] = random.nextInt();
                }
                Arrays.sort(data);
                display.syncExec(() -> {
                    if (table.isDisposed())
                        return;
                    table.setItemCount(data.length);
                    table.clearAll();
                });
                try {
                    Thread.sleep(500);
                } catch (Throwable t) {
                }
            }
        }
    };
    thread.start();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.sourceopen.hadoop.hbase.replication.server.Consumer.java

public static void main(String args[]) {
    try {//from www.  j a  v  a  2s  . c  om
        final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(SPRING_PATH);
        // ?
        Runtime.getRuntime().addShutdownHook(new Thread() {

            @Override
            public void run() {
                try {
                    context.stop();
                    context.close();
                    LOG.info("Consumer server stopped");
                    System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
                            + " Consumer server stoped");
                } catch (Throwable t) {
                    LOG.error("Fail to stop consumer server: ", t);
                }
                synchronized (Consumer.class) {
                    running = false;
                    Consumer.class.notify();
                }

            }
        });
        Configuration conf = HBaseConfiguration.create();
        conf.addResource(ConsumerConstants.COMMON_CONFIG_FILE);
        conf.addResource(ConsumerConstants.CONSUMER_CONFIG_FILE);
        Consumer.start(conf);
        LOG.info("Consumer server started");
        System.out.println(
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " Consumer server started");

    } catch (Throwable t) {
        LOG.error("Fail to start consumer server: ", t);
        System.exit(-1);
    }
    synchronized (Consumer.class) {
        while (running) {
            try {
                Consumer.class.wait();
            } catch (Throwable t) {
                LOG.error("Consumer server got runtime errors: ", t);
            }
        }
    }
}

From source file:com.alibaba.otter.node.deployer.OtterLauncher.java

public static void main(String[] args) throws Throwable {
    // ?dragoon client
    // startDragoon();
    // logger.info("INFO ## the dragoon is start now ......");
    final OtterController controller = OtterContextLocator.getOtterController();
    controller.start();//from  w ww . java2  s  .  co m
    try {
        logger.info("INFO ## the otter server is running now ......");
        Runtime.getRuntime().addShutdownHook(new Thread() {

            public void run() {
                try {
                    logger.info("INFO ## stop the otter server");
                    controller.stop();
                } catch (Throwable e) {
                    logger.warn("WARN ##something goes wrong when stopping Otter Server:\n{}",
                            ExceptionUtils.getFullStackTrace(e));
                } finally {
                    logger.info("INFO ## otter server is down.");
                }
            }

        });
    } catch (Throwable e) {
        logger.error("ERROR ## Something goes wrong when starting up the Otter Server:\n{}",
                ExceptionUtils.getFullStackTrace(e));
        System.exit(0);
    }
}

From source file:com.pinterest.terrapin.server.TerrapinServerMain.java

public static void main(String[] args) {
    PropertiesConfiguration configuration = TerrapinUtil.readPropertiesExitOnFailure(
            System.getProperties().getProperty("terrapin.config", "server.properties"));

    try {//from   ww w .j ava  2  s  .c o m
        final TerrapinServerHandler handler = new TerrapinServerHandler(configuration);
        handler.start();
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                handler.shutdown();
            }
        });
    } catch (Throwable t) {
        LOG.error("Could not start up server.", t);
        System.exit(1);
    }
}

From source file:io.gravitee.gateway.platforms.jetty.bootstrap.Bootstrap.java

public static void main(String[] args) {
    Thread t = Thread.currentThread();
    t.setName("graviteeio-gateway");

    final AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(JettyConfiguration.class);
    ctx.registerShutdownHook();//www .j ava  2 s .  c o  m
    ctx.refresh();

    try {
        final Node node = ctx.getBean(Node.class);
        node.start();

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                LoggerFactory.getLogger(Bootstrap.class).info("Shutting-down Gravitee Gateway...");
                node.stop();
                ctx.close();
            }
        });
    } catch (Exception ex) {
        LOGGER.error("Unable to start Gravitee Gateway", ex);
    }
}

From source file:com.pinterest.terrapin.controller.TerrapinControllerMain.java

public static void main(String[] args) {
    PropertiesConfiguration configuration = TerrapinUtil.readPropertiesExitOnFailure(
            System.getProperties().getProperty("terrapin.config", "controller.properties"));
    try {/*w ww  .  j  ava 2  s .co m*/
        LOG.info("Starting controller.");
        final TerrapinControllerHandler handler = new TerrapinControllerHandler(configuration);
        handler.start();

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                handler.shutdown();
            }
        });
    } catch (Throwable t) {
        LOG.error("Could not start the controller.", t);
    }
}

From source file:MouseEventPost.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    final Button button = new Button(shell, SWT.NONE);
    button.setSize(100, 100);// w  w  w .j  a va  2s  . c  o m
    button.setText("Click");
    shell.pack();
    shell.open();
    button.addListener(SWT.MouseDown, new Listener() {
        public void handleEvent(Event e) {
            System.out.println("Mouse Down (button: " + e.button + " x: " + e.x + " y: " + e.y + ")");
        }
    });
    final Point pt = display.map(shell, null, 50, 50);
    new Thread() {
        Event event;

        public void run() {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
            event = new Event();
            event.type = SWT.MouseMove;
            event.x = pt.x;
            event.y = pt.y;
            display.post(event);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
        }
    }.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:ru.itx.service.ServiceApp.java

public static void main(String[] args) throws Exception {
    final ServiceApp serviceApp = new ServiceApp();
    serviceApp.start();//  w w  w .  j  a  v  a2 s. co m
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            serviceApp.stop();
        }
    });
    Thread.sleep(Long.MAX_VALUE);
}

From source file:com.couragelabs.logging.LogAppenderTestFixture.java

/**
 * Use this method to test the appender. Run this first, then run
 * GlobalContextSocketAppender::main//from   www  .  ja  v a  2  s .  c  o m
 *
 * @param args Program arguments. None are needed.
 * @throws java.lang.Exception if things go wrong
 */
@SuppressWarnings("InfiniteLoopStatement")
public static void main(String[] args) throws Exception {
    ServerSocket serverSocket = new ServerSocket(PORT);

    System.out.println("Starting listen loop.");
    while (true) {
        try {
            final Socket clientSocket = serverSocket.accept();
            System.out.println("Received client connection.");
            new Thread() {
                @Override
                public void run() {
                    ObjectInputStream i = null;
                    try {
                        i = new ObjectInputStream(clientSocket.getInputStream());
                        while (true) {
                            Object received = i.readObject();
                            System.out.println(ToStringBuilder.reflectionToString(received,
                                    ToStringStyle.SHORT_PREFIX_STYLE));
                            Thread.sleep(1000);
                        }
                    } catch (EOFException e) {
                        System.out.println("Client closed connection.");
                    } catch (Throwable t) {
                        t.printStackTrace();
                    } finally {
                        if (i != null) {
                            try {
                                i.close();
                            } catch (Throwable t) {
                                t.printStackTrace();
                            }
                        }
                    }
                }
            }.start();
            Thread.sleep(1000);
        } catch (Throwable t) {
            t.printStackTrace();
        }
        System.out.println("Next...");
    }

}

From source file:org.another.logserver.Starter.java

/**
 *
 * @param args//from   ww  w .  j  ava2 s . co  m
 */
public static void main(String[] args) {

    LOGGER.info("Starting Another Log server ...");

    // Initialize components

    LOGGER.debug("Starting Spring context ...");
    final ApplicationContext springContext = new ClassPathXmlApplicationContext("spring.xml");

    LOGGER.info("Spring intilization: {}", springContext.getStartupDate());

    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            super.run();
            LOGGER.debug("Running Shutdown Hook");

            Configurator conf = springContext.getBean("configurator", Configurator.class);
            for (IEndPoint ep : conf.getConfiguredEndPoints().values()) {
                ep.stop();
            }

        }

    });

    ((AbstractApplicationContext) springContext).registerShutdownHook();

    LOGGER.debug("Entering wait loop");
    try {
        while (true) {
            Thread.sleep(1000 * 60 * 60);
        }
    } catch (Exception e) {
        LOGGER.error("", e);
    }

}