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:BusyCursorDisplay.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    final Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
    text.setLayoutData(new GridData(GridData.FILL_BOTH));
    final int[] nextId = new int[1];
    Button b = new Button(shell, SWT.PUSH);
    b.setText("invoke long running job");
    b.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            Runnable longJob = new Runnable() {
                boolean done = false;

                int id;

                public void run() {
                    Thread thread = new Thread(new Runnable() {
                        public void run() {
                            id = nextId[0]++;
                            display.syncExec(new Runnable() {
                                public void run() {
                                    if (text.isDisposed())
                                        return;
                                    text.append("\nStart long running task " + id);
                                }/*from   ww w. ja v  a  2s .co  m*/
                            });
                            for (int i = 0; i < 100000; i++) {
                                if (display.isDisposed())
                                    return;
                                System.out.println("do task that takes a long time in a separate thread " + id);
                            }
                            if (display.isDisposed())
                                return;
                            display.syncExec(new Runnable() {
                                public void run() {
                                    if (text.isDisposed())
                                        return;
                                    text.append("\nCompleted long running task " + id);
                                }
                            });
                            done = true;
                            display.wake();
                        }
                    });
                    thread.start();
                    while (!done && !shell.isDisposed()) {
                        if (!display.readAndDispatch())
                            display.sleep();
                    }
                }
            };
            BusyIndicator.showWhile(display, longJob);
        }
    });
    shell.setSize(250, 150);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:mini_mirc_client.Mini_mirc_client.java

public static void main(String[] args) {

    try {//from   w w  w  .j a v a 2 s.  co m
        TTransport transport;
        transport = new TSocket("localhost", 2121);

        TProtocol protocol = new TBinaryProtocol(transport);
        miniIRC.Client client = new miniIRC.Client(protocol);

        Runnable updateThread;
        updateThread = new Runnable() {
            public void run() {
                try {
                    while (update) {
                        Thread.sleep(3000);

                        synchronized (transport) {
                            transport.open();
                            updateMsg(client);
                            transport.close();
                        }

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

        new Thread(updateThread).start();

        perform(transport, client);

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

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

public static void main(String[] args) {
    display = new Display();
    shell = new Shell(display);
    shell.setText("Snippet 141");
    shell.setSize(300, 300);/*  www. ja v a2s.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();
    final AtomicBoolean stopAnimation = new AtomicBoolean(false);
    if (fileName != null) {
        loader = new ImageLoader();
        try {
            imageDataArray = loader.load(fileName);
            if (imageDataArray.length > 1) {
                animateThread = new Thread("Animation") {
                    @Override
                    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) && !stopAnimation.get()) {
                                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();
    }
    stopAnimation.set(true);
    display.dispose();
}

From source file:org.apache.streams.datasift.provider.DatasiftManagedSourceSetup.java

public static void main(String[] args) {
    DatasiftManagedSourceSetup job = new DatasiftManagedSourceSetup();
    (new Thread(job)).start();
}

From source file:AnimatedGIFDisplay.java

public static void main(String[] args) {
    display = new Display();
    shell = new Shell(display);
    shell.setSize(300, 300);//w  w  w  .  j  av a  2  s. co m
    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:br.on.daed.services.initializers.SpringBootApplicationInitializer.java

public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            TelaPrincipal telaPrincipal = new TelaPrincipal();
            telaPrincipal.setVisible(true);

            new Thread(new Runnable() {
                public void run() {
                    ConfigurableApplicationContext context = SpringApplication
                            .run(SpringBootApplicationInitializer.class, args);
                    int port = ((TomcatEmbeddedServletContainer) ((AnnotationConfigEmbeddedWebApplicationContext) context)
                            .getEmbeddedServletContainer()).getPort();
                    url = "http://localhost:" + port;
                    telaPrincipal.readyState(url);
                }//from w  ww  .j  a  v a  2  s .c  om
            }).start();
        }
    });
}

From source file:com.senseidb.search.node.inmemory.InMemoryIndexPerfEval.java

public static void main(String[] args) throws Exception {
    final InMemorySenseiService memorySenseiService = InMemorySenseiService.valueOf(
            new File(InMemoryIndexPerfEval.class.getClassLoader().getResource("test-conf/node1/").toURI()));

    final List<JSONObject> docs = new ArrayList<JSONObject>(15000);
    LineIterator lineIterator = FileUtils.lineIterator(
            new File(InMemoryIndexPerfEval.class.getClassLoader().getResource("data/test_data.json").toURI()));
    int i = 0;//  w  w  w .  jav a2 s.  c o m
    while (lineIterator.hasNext() && i < 100) {
        String car = lineIterator.next();
        if (car != null && car.contains("{"))
            docs.add(new JSONObject(car));
        i++;
    }
    Thread[] threads = new Thread[10];
    for (int k = 0; k < threads.length; k++) {
        threads[k] = new Thread(new Runnable() {
            public void run() {
                long time = System.currentTimeMillis();
                //System.out.println("Start thread");
                for (int j = 0; j < 1000; j++) {
                    //System.out.println("Send request");
                    memorySenseiService.doQuery(getRequest(), docs);
                }
                System.out.println("time = " + (System.currentTimeMillis() - time));
            }
        });
        threads[k].start();
    }
    Thread.sleep(500000);
}

From source file:Volatile.java

public static void main(String[] args) {
    try {/*from  w  ww  . j av a2  s  . c  o m*/
        Volatile vol = new Volatile();

        // slight pause to let some time elapse
        Thread.sleep(100);

        Thread t = new Thread(vol);
        t.start();

        // slight pause to allow run() to go first
        Thread.sleep(100);

        vol.workMethod();
    } catch (InterruptedException x) {
        System.err.println("one of the sleeps was interrupted");
    }
}

From source file:org.apache.streams.example.TwitterHistoryElasticsearch.java

public static void main(String[] args) {
    LOGGER.info(StreamsConfigurator.config.toString());

    TwitterHistoryElasticsearch history = new TwitterHistoryElasticsearch();

    new Thread(history).start();

}

From source file:org.apache.streams.elasticsearch.example.ElasticsearchQuery.java

public static void main(String[] args) {
    ElasticsearchQuery job = new ElasticsearchQuery();
    (new Thread(job)).start();

}