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

public static void main(String args[]) {
    Wake queue = new Wake();
    Runnable r = new RunThis(queue);
    Thread t;//w  w w .jav a  2s .  c o  m
    for (int i = 0; i < 10; i++) {
        t = new Thread(r);
        t.start();
    }

    for (int i = 0; i < 11; i++) {
        try {
            Thread.sleep((long) (Math.random() * 1000));
        } catch (InterruptedException e) {
        }
        System.out.println("About to wake one thread");
        queue.wakeOne();
    }
}

From source file:edu.eci.arsw.loannetsim.LoanNetworkSimulation.java

public static void main(String args[]) throws InterruptedException, IOException {
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

    int balancesSum = 0;

    List<Lender> lenders = setupLoanNetwork(NUMOF_LENDERS, ac);

    if (lenders != null) {
        for (Lender im : lenders) {
            new Thread(im).start();
        }/*w w  w  .j  a v a2s.c o  m*/
    }

    while (true) {
        Thread.sleep(10000);

        FixedMoneyLender.pause();

        System.out.println("*** PRESS ENTER TO VIEW STATISTICS ***");

        System.in.read();

        balancesSum = 0;
        for (Lender ln : lenders) {
            balancesSum += ln.getBalance();
        }

        System.out.println("Sum of balances:" + balancesSum);

        System.out.println("Press enter to continue simulation or Ctrl+C to abort...");

        System.in.read();

        FixedMoneyLender.resume();
        synchronized (lenders) {
            lenders.notifyAll();
        }

    }

}

From source file:Main.java

public static void main(String[] arguments) {
    JPanel panel = new JPanel(new BorderLayout());
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(panel);/*from w  w  w .  ja va2 s.  c o  m*/
    frame.setBounds(20, 20, 200, 200);
    frame.setVisible(true);

    JProgressBar progressBar = new JProgressBar();
    progressBar.setIndeterminate(true);
    progressBar.setVisible(false);
    JButton loadButton = new JButton("Load memberlist");
    loadButton.setEnabled(true);
    loadButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    progressBar.setVisible(true);
                    // do my stuff here...
                    try {
                        Thread.sleep(2000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    progressBar.setVisible(false);
                }
            }).start();
        }
    });
    JPanel container = new JPanel(new FlowLayout());
    container.add(loadButton);
    container.add(progressBar);
    panel.add(container);
}

From source file:PiInterrupt.java

public static void main(String[] args) {
    PiInterrupt pi = new PiInterrupt();
    Thread t = new Thread(pi);
    t.start();//from w  w  w .  j  a  va 2 s  .c  om

    try {
        Thread.sleep(10000);
        t.interrupt();
    } catch (InterruptedException x) {
    }
}

From source file:Main.java

public static void main(String[] args) {
    int maximum = 100;
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Integer[] oneRow = { 0, 0, 0, 0 };
    String[] headers = { "A", "B", "C", "D" };
    Integer[][] data = { oneRow, oneRow, oneRow, oneRow, oneRow, };
    DefaultTableModel model = new DefaultTableModel(data, headers);
    JTable table = new JTable(model);
    table.setDefaultRenderer(Object.class, new ProgressRenderer(0, maximum));
    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    frame.add(new JScrollPane(table));
    frame.pack();/*from   w  w w  .j av a 2s.co  m*/
    frame.setVisible(true);
    new Thread(new Runnable() {
        @Override
        public void run() {
            Object waiter = new Object();
            synchronized (waiter) {
                int rows = model.getRowCount();
                int columns = model.getColumnCount();
                Random random = new Random(System.currentTimeMillis());
                boolean done = false;
                while (!done) {
                    int row = random.nextInt(rows);
                    int column = random.nextInt(columns);
                    Integer value = (Integer) model.getValueAt(row, column);
                    value++;
                    if (value <= maximum) {
                        model.setValueAt(value, row, column);
                        try {
                            waiter.wait(15);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    done = true;
                    for (row = 0; row < rows; row++) {
                        for (column = 0; column < columns; column++) {
                            if (!model.getValueAt(row, column).equals(maximum)) {
                                done = false;
                                break;
                            }
                        }
                        if (!done) {
                            break;
                        }
                    }
                }
            }
        }
    }).start();
}

From source file:SyncExecExample.java

public static void main(String[] args) {

    final Display display = new Display();
    Shell shell = new Shell(display);

    final Runnable print = new Runnable() {
        public void run() {
            System.out.println("Print from thread: \t" + Thread.currentThread().getName());
        }//www .jav  a 2  s.co  m
    };

    final Thread applicationThread = new Thread("applicationThread") {
        public void run() {
            System.out.println("Hello from thread: \t" + Thread.currentThread().getName());
            display.syncExec(print);
            System.out.println("Bye from thread: \t" + Thread.currentThread().getName());
        }
    };

    shell.setText("syncExec Example");
    shell.setSize(300, 100);

    Button button = new Button(shell, SWT.CENTER);
    button.setText("Click to start");
    button.setBounds(shell.getClientArea());
    button.addSelectionListener(new SelectionListener() {
        public void widgetDefaultSelected(SelectionEvent e) {
        }

        public void widgetSelected(SelectionEvent e) {
            applicationThread.start();
        }
    });

    shell.open();

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {// If no more entries in event queue
            display.sleep();
        }
    }

    display.dispose();

}

From source file:org.excalibur.overlay.hierarchical.grouping.MemberMain.java

public static void main(String[] args) throws Exception {
    JChannel channel = new JChannel(ClassUtils.getDefaultClassLoader().getResourceAsStream("rest_ping.xml"));
    Group group = new Group(channel);
    channel.connect("exca");

    new Thread(new Runnable() {
        @Override/*from  w w w.  j  ava  2  s. c  o  m*/
        public void run() {
            while (true)
                ;
        }
    }).start();
}

From source file:PrepareProduction.java

public static void main(String[] args) throws Exception {
    BlockingQueue<String> q = new LinkedBlockingQueue<String>();
    Thread p1 = new Thread(new PrepareProduction(q));
    Thread c1 = new Thread(new DoProduction(q));
    p1.start();//from w ww. j a  va  2 s. c o  m
    c1.start();
    p1.join();
    c1.join();
}

From source file:com.surveypanel.form.test.Runner.java

/**
 * @param args//from w  w  w.ja v a2  s. com
 */
public static void main(String[] args) {
    try {
        init(surveyId);
        Thread[] runners = new Thread[100];
        for (int j = 0; j < runners.length; j++) {
            runners[j] = new Thread(new FormRun(formFactory, jsManager, surveyId));
        }
        for (Thread thread : runners) {
            thread.start();
        }
        Thread.sleep(1000);
    } catch (Exception e) {
        log.error(e);
    }
}

From source file:org.apigw.authserver.RemoteCalendar.java

public static void main(String[] args) throws Exception {

    for (int i = 0; i < 5; i++) {
        System.out.println("Starting thread: " + i);
        Thread t = new Thread(new Testing(i));
        t.start();//from  ww w . java2 s.co m
    }

}