Java 多线程 Thread

Java 多线程 Thread

package thread;

/**
 * 继承Thread类创建线程
 * @author add by jzh 2011-12-17
 *
 */
public class SayThread extends Thread {

  public SayThread(String name) {
    super(name);
  }

  public void run() {
    for (int i = 0; i < 5; i++) {
      System.out.println(getName() + " = " + i);
      /*
      try {
        //sleep((int) (Math.random() * 100));
        sleep(100);
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
      }
      */
    }
    System.out.println(getName() + " done");
  }

}
package thread;

public class SayTest {

  public static void main(String args[]) {
    Thread firstThread = new SayThread("firstThread_jzh");
    Thread secondThread = new SayThread("secondThread_jiang");
    Thread threeThread = new SayThread("threeThread_chanpinxue.cn");
    
    firstThread.setPriority(3);
    secondThread.setPriority(10);
    threeThread.setPriority(5);
    
    firstThread.start();
    secondThread.start();
    threeThread.start();
  }
}

 

发表回复

您的电子邮箱地址不会被公开。