public
class Thread implements Runnable {
/* Make sure registerNatives is the first thing <clinit> does. */
private static native void registerNatives();
static {
registerNatives();
}
private volatile String name;
private int priority;
private Thread threadQ;
private long eetop;
public class Task1 implements Runnable {
private int i = 10;
@Override
public void run() {
// TODO Auto-generated method stub
while(i>0) {
System.out.println(Thread.currentThread().getName()+(i--));
}
}
}
测试类
public static void runnableTest() {
Task1 task1 = new Task1();
Thread thread1 = new Thread(task1,"一号");
Thread thread2 = new Thread(task1,"二号");
thread1.start();
thread2.start();
}
继承Thread实现资源共享
public class Task2 extends Thread {
private int i = 10;
@Override
public void run() {
while(i > 0) {
System.out.println(Thread.currentThread().getName()+(i--));
}
}
}
测试类
public static void main(String[] args) {
ThreadTest();
}
public static void ThreadTest() {
Task2 task2 = new Task2();
Thread thread = new Thread(task2,"一号");
Thread thread1 = new Thread(task2,"二号");
thread.start();
thread1.start();
}