new Thread(Runnable target, String name) : Thread « java.lang « Java by API






new Thread(Runnable target, String name)

  
/*
 * Output:
 * 
 * Got: 0
 * Got: 0
 * Got: 0
 * ...
 */

class Queue {
  int n;
  synchronized int get() {
  System.out.println("Got: " + n);
  return n;
  }
  synchronized void put(int n) {
  this.n = n;
  System.out.println("Put: " + n);
  }
}
class Consumer implements Runnable {
  Queue q;
  Consumer(Queue q) {
  this.q = q;
  new Thread(this, "Consumer").start();
  }
  public void run() {
  while(true) {
      q.get();
  }
  }
}
public class MainClass {
  public static void main(String args[]) {
  Queue q = new Queue();
  new Consumer(q);
  }
}


           
         
    
  








Related examples in the same category

1.Thread.MAX_PRIORITY
2.Thread.NORM_PRIORITY
3.Thread.activeCount()
4.Thread.currentThread()
5.Thread.dumpStack()
6.Thread.enumerate(Thread[] tarray)
7.Thread: getStackTrace()
8.Thread: getThreadGroup()
9.Thread: getUncaughtExceptionHandler()
10.Thread: interrupt()
11.Thread: isAlive()
12.Thread: isDaemon()
13.Thread: join() (Using join() to wait for threads to finish)
14.Thread: run()
15.Thread: setDaemon(boolean b)
16.Thread: setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh)
17.Threah.sleep(long millis)
18.Thread: setDaemon(boolean on)
19.Thread: setName(String name)
20.Thread: setPriority(int newPriority)
21.Thread: setUncaughtExceptionHandler(UncaughtExceptionHandler eh)
22.Thread: start
23.Thread: stop
24.implements UncaughtExceptionHandler