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

Source Link

Document

Allocates a new Thread object.

Usage

From source file:Main.java

public static void main(String[] args) {
    new Main().display();
    new Thread(new Runnable() {
        @Override/* www .j  av  a 2 s .  com*/
        public void run() {
            for (int i = 0; i < 3; i++) {
                try {
                    Thread.sleep(1000);
                    System.out.println((i + 1) + "s. elapsed.");
                } catch (InterruptedException e) {
                    e.printStackTrace(System.err);
                }
            }
        }
    }).start();
}

From source file:UnsynchBankTest.java

public static void main(String[] args) {
    Bank b = new Bank(NACCOUNTS, INITIAL_BALANCE);
    int i;/*from   ww  w .j av  a2 s  .co m*/
    for (i = 0; i < NACCOUNTS; i++) {
        TransferRunnable r = new TransferRunnable(b, i, INITIAL_BALANCE);
        Thread t = new Thread(r);
        t.start();
    }
}

From source file:Philosopher.java

public static void main(String[] args) {
    Chopstick[] s = new Chopstick[5];
    Philosopher[] f = new Philosopher[5];
    State hlp = new State();
    for (int i = 0; i < 5; i++) {
        s[i] = new Chopstick();
    }//w  w w . j  ava  2  s.  c om
    for (int i = 0; i < 5; i++) {
        f[i] = new Philosopher(i, s[i], s[(i + 4) % 5], hlp);
        new Thread(f[i]).start();
    }
}

From source file:mase.MaseManagerTerminal.java

public static void main(String[] args) {
    File r = null, j = null;//  www  . ja  v  a2 s .com
    if (args.length > 0) {
        r = new File(args[0]);
    }
    if (args.length > 1) {
        j = new File(args[1]);
    }

    final MaseManager manager = new MaseManager();
    final MaseManagerTerminal terminal = new MaseManagerTerminal(manager);
    manager.setStatusListener(terminal);

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
            terminal.message("Exiting gracefully");
            manager.cancelAll();
        }
    }));

    manager.startExecution();
    terminal.run(r, j);
}

From source file:Snippet141.java

public static void main(String[] args) {
    display = new Display();
    shell = new Shell(display);
    shell.setSize(300, 300);/*from w w w.  j a va 2s  . c  om*/
    shell.open();
    shellGC = new GC(shell);
    shellBackground = shell.getBackground();

    FileDialog dialog = new FileDialog(shell);
    dialog.setFilterExtensions(new String[] { "*.gif" });
    String fileName = dialog.open();
    if (fileName != null) {
        loader = new ImageLoader();
        try {
            imageDataArray = loader.load(fileName);
            if (imageDataArray.length > 1) {
                animateThread = new Thread("Animation") {
                    public void run() {
                        /* Create an off-screen image to draw on, and fill it with the shell background. */
                        Image offScreenImage = new Image(display, loader.logicalScreenWidth,
                                loader.logicalScreenHeight);
                        GC offScreenImageGC = new GC(offScreenImage);
                        offScreenImageGC.setBackground(shellBackground);
                        offScreenImageGC.fillRectangle(0, 0, loader.logicalScreenWidth,
                                loader.logicalScreenHeight);

                        try {
                            /* Create the first image and draw it on the off-screen image. */
                            int imageDataIndex = 0;
                            ImageData imageData = imageDataArray[imageDataIndex];
                            if (image != null && !image.isDisposed())
                                image.dispose();
                            image = new Image(display, imageData);
                            offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height,
                                    imageData.x, imageData.y, imageData.width, imageData.height);

                            /* Now loop through the images, creating and drawing each one
                             * on the off-screen image before drawing it on the shell. */
                            int repeatCount = loader.repeatCount;
                            while (loader.repeatCount == 0 || repeatCount > 0) {
                                switch (imageData.disposalMethod) {
                                case SWT.DM_FILL_BACKGROUND:
                                    /* Fill with the background color before drawing. */
                                    Color bgColor = null;
                                    if (useGIFBackground && loader.backgroundPixel != -1) {
                                        bgColor = new Color(display,
                                                imageData.palette.getRGB(loader.backgroundPixel));
                                    }
                                    offScreenImageGC.setBackground(bgColor != null ? bgColor : shellBackground);
                                    offScreenImageGC.fillRectangle(imageData.x, imageData.y, imageData.width,
                                            imageData.height);
                                    if (bgColor != null)
                                        bgColor.dispose();
                                    break;
                                case SWT.DM_FILL_PREVIOUS:
                                    /* Restore the previous image before drawing. */
                                    offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height,
                                            imageData.x, imageData.y, imageData.width, imageData.height);
                                    break;
                                }

                                imageDataIndex = (imageDataIndex + 1) % imageDataArray.length;
                                imageData = imageDataArray[imageDataIndex];
                                image.dispose();
                                image = new Image(display, imageData);
                                offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height,
                                        imageData.x, imageData.y, imageData.width, imageData.height);

                                /* Draw the off-screen image to the shell. */
                                shellGC.drawImage(offScreenImage, 0, 0);

                                /* Sleep for the specified delay time (adding commonly-used slow-down fudge factors). */
                                try {
                                    int ms = imageData.delayTime * 10;
                                    if (ms < 20)
                                        ms += 30;
                                    if (ms < 30)
                                        ms += 10;
                                    Thread.sleep(ms);
                                } catch (InterruptedException e) {
                                }

                                /* If we have just drawn the last image, decrement the repeat count and start again. */
                                if (imageDataIndex == imageDataArray.length - 1)
                                    repeatCount--;
                            }
                        } catch (SWTException ex) {
                            System.out.println("There was an error animating the GIF");
                        } finally {
                            if (offScreenImage != null && !offScreenImage.isDisposed())
                                offScreenImage.dispose();
                            if (offScreenImageGC != null && !offScreenImageGC.isDisposed())
                                offScreenImageGC.dispose();
                            if (image != null && !image.isDisposed())
                                image.dispose();
                        }
                    }
                };
                animateThread.setDaemon(true);
                animateThread.start();
            }
        } catch (SWTException ex) {
            System.out.println("There was an error loading the GIF");
        }
    }

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:de.fionera.javamailer.main.Main.java

/**
 * The Main Class which will be started//from w w  w  .  j  a v a 2s.  c om
 *
 * @param args
 */
public static void main(String[] args) {
    ObjectMapper mapper = new ObjectMapper();
    File file = new File("settings.jm");

    try {
        loadSettings(file, mapper);
    } catch (Exception e) {
        e.printStackTrace();
    }

    controllerMain controllerMain = new controllerMain();
    controllerMain.createMainView();

    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        saveSettings(file, mapper);
    }));
}

From source file:com.usefullc.solm.common.proxy.nio.NIOSocketServer.java

public static void main(String[] args) {
    try {/*from w w  w  .  j  a v a 2 s . c o m*/
        //proxy connect pool
        System.setProperty("htmlDir", args[0]);
        int port = Integer.valueOf(args[1]);
        ServerMgr.init(port);
        //end

        System.out.println("htmlDir=" + args[0]);
        System.out.println("port=" + args[1]);

        ProxyTaskExecutor.start(); //?

        new Thread(new Runnable() {
            @Override
            public void run() {
                ServerMgr.startServer();

                //                    ReadHandler.start();  //?browser req and server res ?
            }
        }).start();

        System.out.println("server is start at " + ServerMgr.getPort());

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    new Thread(() -> {
        int submitCount = 0;
        while (submitCount < 10) {
            if (!updating) {
                futureList.add(pool.submit(callable));
                submitCount++;//w  ww.jav  a 2s  .  c om
            }
            try {
                Thread.sleep(1000); // arbitrary
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
    // update thread
    new Thread(() -> {
        int updateCount = 0;
        while (updateCount < 5) {
            doUpdate();
            updateCount++;
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
}

From source file:com.blacklocus.qs.worker.RandomStdoutTasksExample.java

public static void main(String[] args) {
    // Mock our source of tasks.
    final BlockingQueue<QSTaskModel> workQueue = new SynchronousQueue<QSTaskModel>();

    // Generates tasks
    new Thread(new ExceptingRunnable() {
        @Override/*  w w  w . ja  v  a 2s . c om*/
        protected void go() throws Exception {
            while (true) {
                workQueue.put(new QSTaskModel(null, "" + RandomUtils.nextInt(), "stdout", 1,
                        new Params(RandomStringUtils.randomAscii(RandomUtils.nextInt(32)))));
            }
        }
    }).start();

    // All this worker does is log an extra message describing the length of the "message" param.
    QSWorker<Params> worker = new AbstractQSWorker<Params>() {
        @Override
        public String getHandlerName() {
            // This identifies the type of task this worker can handle. In our task generator above, the
            // tasks are created with the same handler identifier "stdout".
            return "stdout";
        }

        @Override
        public TaskKit<Params> convert(TaskKitFactory<Params> factory) throws Exception {
            return factory.newTaskKit(Params.class);
        }

        @Override
        public Object process(TaskKit<Params> kit) throws Exception {
            String msg = kit.params().message;
            kit.log(msg + " is " + msg.length() + " characters long");
            return null;
        }
    };

    QSAssembly.newBuilder()

            // The source of work.
            .taskServices(new BlockingQueueQSTaskService(workQueue))

            // Logging service which records task start, task-specific logging, task end.
            .logService(new SystemOutQSLogService())

            // Service that helps identify the machine completing tasks, this machine.
            .workerIdService(new HostNameQSWorkerIdService())

            // The worker logic observed by this instance.
            .workers(worker)

            // Run it in the current thread.
            .build().run();
}

From source file:GeneralInterrupt.java

public static void main(String[] args) {
    GeneralInterrupt si = new GeneralInterrupt();
    Thread t = new Thread(si);
    t.start();/*from  w w  w .jav  a2s  .  c o m*/

    try {
        Thread.sleep(2000);
    } catch (InterruptedException x) {
    }

    System.out.println("in main() - interrupting other thread");
    t.interrupt();
    System.out.println("in main() - leaving");
}