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

public static void main(String args[]) {
    map = new WeakHashMap();
    map.put("A", "B");
    Runnable runner = new Runnable() {
        public void run() {
            while (map.containsKey("A")) {
                try {
                    Thread.sleep(1000);
                    System.gc();/*  www.  j  a  v a 2s.co  m*/
                } catch (InterruptedException ignored) {
                }
                System.out.println("Has A");
                System.gc();
            }
        }
    };
    Thread t = new Thread(runner);
    t.start();
    System.out.println("Main waiting");
    try {
        t.join();
    } catch (InterruptedException ignored) {
    }
    System.gc();
}

From source file:MyThread.java

public static void main(String args[]) {
    System.out.println("Main thread starting.");
    MyThread mt = new MyThread();
    Thread newThrd = new Thread(mt);
    newThrd.start();// w ww .ja  v a 2  s. com
    do {
        System.out.println("In main thread.");
        try {
            Thread.sleep(250);
        } catch (InterruptedException exc) {
            System.out.println("Main thread interrupted.");
        }
    } while (mt.count != 5);

    System.out.println("Main thread ending.");
}

From source file:TryThread.java

public static void main(String[] args) {
    Thread first = new Thread(new TryThread("A ", "a ", 200L));
    Thread second = new Thread(new TryThread("B ", "b ", 300L));
    Thread third = new Thread(new TryThread("C ", "c ", 500L));
    System.out.println("Press Enter when you have had enough...\n");
    first.start();/*from w w  w  .  jav  a2  s.  c o  m*/
    second.start();
    third.start();
    try {
        System.in.read();
        System.out.println("Enter pressed...\n");
    } catch (IOException e) {
        System.out.println(e);
    }
    System.out.println("Ending main()");
    return;
}

From source file:Main.java

public static void main(String[] args) {
    Main temp = new Main();
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(temp);//from   w ww  . j  a v a2s .  c  om
    frame.pack();
    frame.setVisible(true);
    Thread updater = new Thread(temp.new CustomThread());
    updater.start();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ServerSocket serverSocket = new ServerSocket(12900, 100, InetAddress.getByName("localhost"));
    System.out.println("Server started  at:  " + serverSocket);

    while (true) {
        System.out.println("Waiting for a  connection...");

        final Socket activeSocket = serverSocket.accept();

        System.out.println("Received a  connection from  " + activeSocket);
        Runnable runnable = () -> handleClientRequest(activeSocket);
        new Thread(runnable).start(); // start a new thread
    }/*  w  w w.  java 2 s. c  o  m*/
}

From source file:PrepareProduction.java

public static void main(String[] args) throws Exception {
    List q = Collections.synchronizedList(new LinkedList<String>());
    Thread p1 = new Thread(new PrepareProduction(q));
    Thread c1 = new Thread(new DoProduction(q));
    p1.start();/* w  w  w.  j av a  2s.c  om*/
    c1.start();
    p1.join();
    c1.join();
}

From source file:AlternateStop.java

public static void main(String[] args) {
    AlternateStop as = new AlternateStop();
    Thread t = new Thread(as);
    t.start();//from   w w  w  . jav  a  2  s  .  c  o m

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

From source file:AnimationTester.java

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

    ArrayComponent panel = new ArrayComponent();
    frame.add(panel, BorderLayout.CENTER);

    frame.setSize(800, 300);/*  w ww. j  av a 2 s .  co  m*/
    frame.setVisible(true);

    Double[] values = new Double[100];
    for (int i = 0; i < values.length; i++)
        values[i] = Math.random() * panel.getHeight();

    final Sorter sorter = new Sorter(values, panel);

    Thread sorterThread = new Thread(sorter);
    sorterThread.start();
}

From source file:nayan.netty.client.FileUploadClient.java

public static void main(String args[]) {

    for (int i = 0; i < CLIENT_COUNT; i++) {
        new Thread(new Runnable() {

            @Override/*from  w  w w  .  j a  v a 2 s  . c  o  m*/
            public void run() {
                try {
                    uploadFile();
                } catch (Exception ex) {
                    Logger.getLogger(FileUploadClient.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }).start();

    }

}

From source file:Main.java

public static void main(String[] args) {
    JFrame parentFrame = new JFrame();
    parentFrame.setSize(500, 150);//from w w  w  . j  av a 2  s .  c om
    JLabel jl = new JLabel();
    jl.setText("Count : 0");

    parentFrame.add(BorderLayout.CENTER, jl);
    parentFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    parentFrame.setVisible(true);

    final JDialog dlg = new JDialog(parentFrame, "Progress Dialog", true);
    JProgressBar dpb = new JProgressBar(0, 500);
    dlg.add(BorderLayout.CENTER, dpb);
    dlg.add(BorderLayout.NORTH, new JLabel("Progress..."));
    dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    dlg.setSize(300, 75);
    dlg.setLocationRelativeTo(parentFrame);

    Thread t = new Thread(new Runnable() {
        public void run() {
            dlg.setVisible(true);
        }
    });
    t.start();
    for (int i = 0; i <= 500; i++) {
        jl.setText("Count : " + i);
        dpb.setValue(i);
        if (dpb.getValue() == 500) {
            dlg.setVisible(false);
            System.exit(0);

        }
        try {
            Thread.sleep(25);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    dlg.setVisible(true);
}