1 package cn.itcast; 2 3 import java.util.Stack; 4 5 class DoSomething implements Runnable { 6 private String name; 7 8 public DoSomething(String name) { 9 this.name = name; 10 } 11 12 public void run() { 13 // TODO Auto-generated method stub 14 for(int i=0;i<5;i++){ 15 for(int k=0;k<20;k++){ 16 System.out.println(name+": "+i); 17 } 18 } 19 } 20 } 21 22 public class Test01 extends Thread { 23 public static void main(String[] args) { 24 DoSomething ds1 = new DoSomething("zhang san"); 25 DoSomething ds2 = new DoSomething("li si"); 26 27 Thread t1 = new Thread(ds1); 28 Thread t2 = new Thread(ds2); 29 t1.start(); 30 t2.start(); 31 } 32 33 }
- Thread.yield()方法作用是:暂停当前正在执行的线程对象,并执行其他线程。
- 设置线程的优先级:线程默认的优先级是创建它的执行线程的优先级。可以通过setPriority(int newPriority)更改线程的优先级。
- Thread的非静态方法join()让一个线程B“加入”到另外一个线程A的尾部