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.Snippet146.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 146");
    final Text text = new Text(shell, SWT.BORDER);
    text.setSize(text.computeSize(150, SWT.DEFAULT));
    shell.pack();//  w  w w.  j  a v a  2 s.c  o m
    shell.open();
    new Thread() {
        @Override
        public void run() {
            // wait for shell to be opened
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }

            String string = "Love the method.";
            for (int i = 0; i < string.length(); i++) {
                char ch = string.charAt(i);
                boolean shift = Character.isUpperCase(ch);
                ch = Character.toLowerCase(ch);
                if (shift) {
                    Event event = new Event();
                    event.type = SWT.KeyDown;
                    event.keyCode = SWT.SHIFT;
                    display.post(event);
                }
                Event event = new Event();
                event.type = SWT.KeyDown;
                event.character = ch;
                display.post(event);
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                }
                event.type = SWT.KeyUp;
                display.post(event);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
                if (shift) {
                    event = new Event();
                    event.type = SWT.KeyUp;
                    event.keyCode = SWT.SHIFT;
                    display.post(event);
                }
            }
        }
    }.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:gridool.GridMain.java

public static void main(String[] args) {
    final Thread parent = Thread.currentThread();
    final GridServer grid = new GridServer();
    try {/*w w w .j a va  2 s  .co  m*/
        grid.start();
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                parent.interrupt();
            }
        });
    } catch (InternalException ie) {
        shutdown(grid, ie);
    } catch (Throwable e) {
        shutdown(grid, e);
    }
    try {
        Thread.currentThread().join();
    } catch (InterruptedException e) {
        ;
    } finally {
        try {
            grid.shutdown(true);
        } catch (RemoteException re) {
            ;
        }
    }
}

From source file:KeyEventPost.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    final Text text = new Text(shell, SWT.BORDER);
    text.setSize(text.computeSize(150, SWT.DEFAULT));
    shell.pack();//from w ww. j  a v  a  2s .c om
    shell.open();
    new Thread() {
        public void run() {
            String string = "Love the method.";
            for (int i = 0; i < string.length(); i++) {
                char ch = string.charAt(i);
                boolean shift = Character.isUpperCase(ch);
                ch = Character.toLowerCase(ch);
                if (shift) {
                    Event event = new Event();
                    event.type = SWT.KeyDown;
                    event.keyCode = SWT.SHIFT;
                    display.post(event);
                }
                Event event = new Event();
                event.type = SWT.KeyDown;
                event.character = ch;
                display.post(event);
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                }
                event.type = SWT.KeyUp;
                display.post(event);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
                if (shift) {
                    event = new Event();
                    event.type = SWT.KeyUp;
                    event.keyCode = SWT.SHIFT;
                    display.post(event);
                }
            }
        }
    }.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:InvokeExample.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel p = new JPanel();
    p.add(good);/*from  w  ww  . j  av a  2 s  .  c om*/
    p.add(bad);
    p.add(bad2);

    Container c = f.getContentPane();
    c.setLayout(new BorderLayout());
    c.add(p, BorderLayout.CENTER);
    c.add(resultLabel, BorderLayout.SOUTH);

    good.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            resultLabel.setText("Working . . .");
            setEnabled(false);
            Thread worker = new Thread() {
                public void run() {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException ex) {
                    }

                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            resultLabel.setText("Ready");
                            setEnabled(true);
                        }
                    });
                }
            };
            worker.start();
        }
    });

    bad.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            resultLabel.setText("Working . . .");
            setEnabled(false);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ex) {
            }
            resultLabel.setText("Ready");
            setEnabled(true);
        }
    });

    bad2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            resultLabel.setText("Working . . . ");
            setEnabled(false);
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Thread.sleep(5000); // Dispatch thread is starving!
                    } catch (InterruptedException ex) {
                    }

                    resultLabel.setText("Ready");
                    setEnabled(true);
                }
            });
        }
    });

    f.setSize(300, 100);
    f.setVisible(true);
}

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

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 56");
    final ProgressBar bar = new ProgressBar(shell, SWT.SMOOTH);
    Rectangle clientArea = shell.getClientArea();
    bar.setBounds(clientArea.x, clientArea.y, 200, 32);
    shell.open();/* w w w . j a v  a2  s  .  c  o  m*/
    final int maximum = bar.getMaximum();
    new Thread() {
        @Override
        public void run() {
            for (final int[] i = new int[1]; i[0] <= maximum; i[0]++) {
                try {
                    Thread.sleep(100);
                } catch (Throwable th) {
                }
                if (display.isDisposed())
                    return;
                display.asyncExec(() -> {
                    if (bar.isDisposed())
                        return;
                    bar.setSelection(i[0]);
                });
            }
        }
    }.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:TableFillThread.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Lazy Table");
    shell.setLayout(new FillLayout());
    final Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
    table.setSize(200, 200);// w  w  w  . j  ava  2s  .  c om

    Thread thread = new Thread() {
        public void run() {
            for (int i = 0; i < 20000; i++) {
                final int[] index = new int[] { i };
                display.syncExec(new Runnable() {
                    public void run() {
                        if (table.isDisposed())
                            return;
                        TableItem item = new TableItem(table, SWT.NONE);
                        item.setText("Table Item " + index[0]);
                    }
                });
            }
        }
    };
    thread.start();
    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:com.linkedin.pinot.broker.broker.BrokerServerBuilderTest.java

public static void main(String[] args) throws Exception {
    PropertiesConfiguration config = new PropertiesConfiguration(
            new File(BrokerServerBuilderTest.class.getClassLoader().getResource("broker.properties").toURI()));
    final BrokerServerBuilder bld = new BrokerServerBuilder(config, null, null, null);
    bld.buildNetwork();// w  ww  .  j a  v a2 s  .c  om
    bld.buildHTTP();
    bld.start();

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            try {
                bld.stop();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    while (true) {
        String command = br.readLine();
        if (command.equals("exit")) {
            bld.stop();
        }
    }

}

From source file:me.ryandowling.TwitchTools.java

public static void main(String[] args) {
    loadSettings();/*  w w  w .j av  a  2 s.c  o m*/

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            saveSettings();
        }
    });

    if (args.length == 0) {
        System.err.println("Invalid number of arguments specified!");
        System.exit(1);
    } else if (args.length >= 1 && args.length <= 4) {
        switch (args[0]) {
        case "Followers":
            if (args.length == 4) {
                new Followers(args[1], Integer.parseInt(args[2]), Boolean.parseBoolean(args[3])).run();
            } else {
                System.err.println("Invalid number of arguments specified!");
                System.exit(1);
            }
            break;
        case "MicrophoneStatus":
            if (args.length == 3) {
                final int delay = Integer.parseInt(args[1]);
                final boolean guiDisplay = Boolean.parseBoolean(args[2]);

                new MicrophoneStatus(delay, guiDisplay);
            } else {
                System.err.println("Invalid number of arguments specified!");
                System.err.println("Arguments are: [delay in ms for updates] [if the gui should show]!");
                System.err.println("For example: [100] [true]!");
                System.exit(1);
            }
            break;
        case "NowPlayingConverter":
            if (args.length == 2) {
                final int delay = Integer.parseInt(args[1]);
                new NowPlayingConverter(delay).run();
            } else {
                System.err.println("Invalid number of arguments specified!");
                System.err.println("Arguments are: [delay in ms for updates]!");
                System.err.println("For example: [100]!");
                System.exit(1);
            }
            break;
        case "MusicCreditsGenerator":
            if (args.length == 2) {
                final String type = args[1];

                if (type.equalsIgnoreCase("html") || type.equalsIgnoreCase("october")
                        || type.equalsIgnoreCase("markdown")) {
                    new MusicCreditsGenerator(type).run();
                } else {
                    System.err.println("Invalid type argument specified!");
                    System.err.println("Arguments are: [type of output to generate (html|markdown|october)]!");
                    System.err.println("For example: [html]!");
                    System.exit(1);
                }
            } else {
                System.err.println("Invalid number of arguments specified!");
                System.err.println("Arguments are: [delay in ms for updates]!");
                System.err.println("For example: [100]!");
                System.exit(1);
            }
            break;
        case "SteamGamesGenerator":
            if (args.length == 2) {
                final String type = args[1];

                if (type.equalsIgnoreCase("october")) {
                    new SteamGamesGenerator(type).run();
                } else {
                    System.err.println("Invalid type argument specified!");
                    System.err.println("Arguments are: [type of output to generate (october)]!");
                    System.err.println("For example: [october]!");
                    System.exit(1);
                }
            } else {
                System.err.println("Invalid number of arguments specified!");
                System.err.println("Arguments are: [delay in ms for updates]!");
                System.err.println("For example: [100]!");
                System.exit(1);
            }
            break;
        case "FoobarControls":
            if (args.length == 1) {
                new FoobarControls().run();
            } else {
                System.err.println("Invalid number of arguments specified!");
                System.err.println("There are no arguments to provide!");
                System.exit(1);
            }
            break;
        default:
            System.err.println("Invalid tool name specified!");
            System.exit(1);
        }
    }
}

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

public static void main(String[] args) {
    final Display display = new Display();
    final Image image = new Image(display, 16, 16);
    GC gc = new GC(image);
    gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
    gc.fillRectangle(image.getBounds());
    gc.dispose();// w  ww .  j a v  a 2  s.  c o m
    final Shell shell = new Shell(display);
    shell.setText("Lazy Table");
    shell.setLayout(new FillLayout());
    final Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
    table.setSize(200, 200);
    Thread thread = new Thread() {
        @Override
        public void run() {
            for (int i = 0; i < 20000; i++) {
                if (table.isDisposed())
                    return;
                final int[] index = new int[] { i };
                display.syncExec(() -> {
                    if (table.isDisposed())
                        return;
                    TableItem item = new TableItem(table, SWT.NONE);
                    item.setText("Table Item " + index[0]);
                    item.setImage(image);
                });
            }
        }
    };
    thread.start();
    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    image.dispose();
    display.dispose();
}

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

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 142");
    final Button button = new Button(shell, SWT.NONE);
    button.setSize(100, 100);/*from  w  w  w .  j  a v a 2 s  . c  o m*/
    button.setText("Click");
    shell.pack();
    shell.open();
    button.addListener(SWT.MouseDown,
            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;

        @Override
        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) {
            }
            event.type = SWT.MouseDown;
            event.button = 1;
            display.post(event);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
            event.type = SWT.MouseUp;
            display.post(event);
        }
    }.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}