java thread

来源:百度知道 编辑:UC知道 时间:2024/06/06 04:02:06
经典代码举例

package threadgroup;

class ThreadDemo3 extends Thread {
private String name;
private int delay;

public ThreadDemo3(String sname, int i_delay) {
name = sname;
delay = i_delay;

}

public void run() {
try {
sleep(delay);
} catch (InterruptedException e) {

}
System.out.println("多线程测试!\n" + name + "\n" + delay);
}

}

public class testMyThread {

public static void main(String[] args) {
ThreadDemo3 th1,th2,th3;

th1 = new ThreadDemo3("线程1", (int) (Math.random() * 900));
th2 = new ThreadDemo3("线程2", (int) (Math.random() * 900));
th3 = new ThreadDemo3("线程3", (int) (Math.random() * 900));
th1.start();
th2.start();
th3.start();

}
}

package threadgroup;

public class threadDemo {
public static void m