package david;

public class Example1 extends Thread  {

 /**
  * 通过继承线程类, 重写run方法.
  */
 public void run(){
  System.out.println("thread begin!");
 }
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
        Example1 examp = new Example1();
        examp.start();
 }

}

-----

package david;

public class Example2 implements Runnable {

 /**
  * 通过实现Runnable接口, 完成run方法.
  */
 public static void main(String[] args) {
  Example2 examp = new Example2();
  Thread thread = new Thread(examp);
        thread.start();
 }

 public void run() {
  System.out.println("thread begin!");
  
 }

}

----

package david;

public class Thread1 implements Runnable {
 /**
  * synchronized同步锁
  */
 public void run() {
  synchronized (this) {
   for (int i = 0; i < 5; i++) {
    System.out.println(Thread.currentThread().getName()
      + " synchronized loop " + i);
   }
  }
 }

 public static void main(String[] args) {
  Thread1 t1 = new Thread1();
  Thread ta = new Thread(t1, "A");
  Thread tb = new Thread(t1, "B");
  ta.start();
  tb.start();
 }
}