Example usage for java.lang Thread start

List of usage examples for java.lang Thread start

Introduction

In this page you can find the example usage for java.lang Thread start.

Prototype

public synchronized void start() 

Source Link

Document

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

Usage

From source file:EarlyNotify.java

public static void main(String[] args) {
    final EarlyNotify enf = new EarlyNotify();

    Runnable runA = new Runnable() {
        public void run() {
            try {
                String item = enf.removeItem();
                print("returned: '" + item + "'");
            } catch (InterruptedException ix) {
                print("interrupted!");
            } catch (Exception x) {
                print("threw an Exception!!!\n" + x);
            }/*  w ww  .  j  a v a  2  s . c o m*/
        }
    };

    Runnable runB = new Runnable() {
        public void run() {
            enf.addItem("Hello!");
        }
    };

    try {
        Thread threadA1 = new Thread(runA, "A");
        threadA1.start();

        Thread.sleep(500);

        Thread threadA2 = new Thread(runA, "B");
        threadA2.start();

        Thread.sleep(500);

        Thread threadB = new Thread(runB, "C");
        threadB.start();

        Thread.sleep(1000);

        threadA1.interrupt();
        threadA2.interrupt();
    } catch (InterruptedException x) {
    }
}

From source file:ProducerComsumer.java

public static void main(String[] args) {
    final ProducerComsumer ch = new ProducerComsumer();

    Runnable runA = new Runnable() {
        public void run() {
            try {
                String str;//from  www .  j  a v  a2 s  .c o m
                Thread.sleep(500);

                str = "multithreaded";
                ch.putIn(str);
                str = "programming";
                ch.putIn(str);

                str = "with Java";
                ch.putIn(str);
            } catch (InterruptedException x) {
                x.printStackTrace();
            }
        }
    };

    Runnable runB = new Runnable() {
        public void run() {
            try {
                Object obj;

                obj = ch.takeOut();
                System.out.println("in run() - just took out: '" + obj + "'");

                Thread.sleep(500);

                obj = ch.takeOut();
                System.out.println("in run() - just took out: '" + obj + "'");

                obj = ch.takeOut();
                System.out.println("in run() - just took out: '" + obj + "'");
            } catch (InterruptedException x) {
                x.printStackTrace();
            }
        }
    };

    Thread threadA = new Thread(runA, "threadA");
    threadA.start();

    Thread threadB = new Thread(runB, "threadB");
    threadB.start();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Semaphore sem = new Semaphore(1, true);
    Thread thrdA = new Thread(new MyThread(sem, "Message 1"));
    Thread thrdB = new Thread(new MyThread(sem, "Message 2"));

    thrdA.start();
    thrdB.start();/*from www.  j a va2s .c  o m*/

    thrdA.join();
    thrdB.join();

}

From source file:WaitComm.java

public static void main(String args[]) {
    WFlagSend s = new WFlagSend();
    WFlagRec r = new WFlagRec(s);
    Thread st = new Thread(s);
    Thread rt = new Thread(r);
    rt.setDaemon(true);/*from  w w w  .  ja  va2  s  .c  o  m*/
    st.start();
    rt.start();
    try {
        st.join();
        while (s.isValid) {
            Thread.sleep(100);
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:MainClass.java

  public static void main(String[] args) {
  Thread first = new TryThread("A ", "a ", 200L);
  Thread second = new TryThread("B ", "b ", 300L);
  Thread third = new TryThread("C ", "c ", 500L);
  first.start();
  second.start();//from  w w  w . j  a v a  2  s  .c  om
  third.start();
  try {
    first.join();
  } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Semaphore sem = new Semaphore(1, true);
    Thread thrdA = new Thread(new SyncOutput(sem, "Message 1"));
    Thread thrdB = new Thread(new SyncOutput(sem, "Message 2!"));

    thrdA.start();
    thrdB.start();//w  w w .jav  a2 s  . c o m

    thrdA.join();
    thrdB.join();

}

From source file:ThreadDemo.java

public static void main(String args[]) {
    Thread t = new Thread(new ThreadDemo());
    System.out.println("Executing " + t.getName());
    // this will call run() fucntion
    t.start();

    // interrupt the threads
    if (!t.interrupted()) {
        t.interrupt();/*from  w  w  w . j  a v a 2 s.  com*/
    }
    // block until other threads finish
    try {
        t.join();
    } catch (InterruptedException e) {
    }
}

From source file:Deadlock.java

public static void main(String[] args) {
    final Deadlock obj1 = new Deadlock("Thread 1");
    final Deadlock obj2 = new Deadlock("Thread 2");

    Runnable runA = new Runnable() {
        public void run() {
            obj1.checkOther(obj2);/*from  www  . jav  a 2s  .  c om*/
        }
    };

    Thread thread = new Thread(runA, "A");
    thread.start();

    try {
        Thread.sleep(200);
    } catch (InterruptedException x) {
    }

    Runnable runB = new Runnable() {
        public void run() {
            obj2.checkOther(obj1);
        }
    };

    Thread threadB = new Thread(runB, "B");
    threadB.start();

    try {
        Thread.sleep(5000);
    } catch (InterruptedException x) {
    }

    threadPrint("finished sleeping");

    threadPrint("about to interrupt() threadA");
    thread.interrupt();

    try {
        Thread.sleep(1000);
    } catch (InterruptedException x) {
    }

    threadPrint("about to interrupt() threadB");
    threadB.interrupt();

    try {
        Thread.sleep(1000);
    } catch (InterruptedException x) {
    }

    threadPrint("did that break the deadlock?");
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    float sampleRate = 8000;
    int sampleSizeInBits = 8;
    int channels = 1;
    boolean signed = true;
    boolean bigEndian = true;
    final AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
    DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
    final TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
    line.open(format);//from  ww w. j a v a  2 s  .  co  m
    line.start();
    Runnable runner = new Runnable() {
        int bufferSize = (int) format.getSampleRate() * format.getFrameSize();

        byte buffer[] = new byte[bufferSize];

        public void run() {
            try {

                int count = line.read(buffer, 0, buffer.length);
                if (count > 0) {
                    out.write(buffer, 0, count);
                }

                out.close();
            } catch (IOException e) {
                System.err.println("I/O problems: " + e);
                System.exit(-1);
            }
        }
    };
    Thread captureThread = new Thread(runner);
    captureThread.start();

    byte audio[] = out.toByteArray();
    InputStream input = new ByteArrayInputStream(audio);
    final SourceDataLine line1 = (SourceDataLine) AudioSystem.getLine(info);
    final AudioInputStream ais = new AudioInputStream(input, format, audio.length / format.getFrameSize());
    line1.open(format);
    line1.start();

    runner = new Runnable() {
        int bufferSize = (int) format.getSampleRate() * format.getFrameSize();

        byte buffer[] = new byte[bufferSize];

        public void run() {
            try {
                int count;
                while ((count = ais.read(buffer, 0, buffer.length)) != -1) {
                    if (count > 0) {
                        line1.write(buffer, 0, count);
                    }
                }
                line1.drain();
                line1.close();
            } catch (IOException e) {
                System.err.println("I/O problems: " + e);
                System.exit(-3);
            }
        }
    };
    Thread playThread = new Thread(runner);
    playThread.start();

}

From source file:com.yahoo.ads.pb.mttf.PistachiosBenchmarking.java

public static void main(String[] args) {
    int threadCount = 50;
    logger.info("parsing error {}", args.length);
    if (args.length >= 1) {
        try {// www  .  j a v  a  2  s.  com
            threadCount = Integer.parseInt(args[0]);
            logger.info("parsd {} {}", args[0], threadCount);
        } catch (Exception e) {
            logger.info("parsing error", e);
        }
    }
    if (args.length >= 2) {
        try {
            recordAverageSize = Integer.parseInt(args[1]);
            logger.info("parsd {} {}", args[1], recordAverageSize);
        } catch (Exception e) {
            logger.info("parsing error", e);
        }
    }

    for (int i = 0; i < threadCount; i++) {
        Thread thread = new Thread(new PistachiosBenchmarking(), "benchmarking" + i);
        thread.start();
    }

}