public class Test extends Object {
public static void main(String[] args) throws Exception {
/*
命名操作:
在线程启动前命名,尽量不重名,线程运行时不改名
1. public Thread(Runnable target, String name)
2. public final String getName()
3. public final synchronized void setName(String name)
4. public static native Thread currentThread()
*/
Thread thread0 = new Thread(new MyThread(), "自己命名的线程0");
Thread thread1 = new Thread(new MyThread());
thread1.setName("自己命名的线程1");
Thread thread2 = new Thread(new MyThread());
Thread thread3 = new Thread(new MyThread());
/*
线程优先级 1-10
public final static int MIN_PRIORITY = 1;
public final static int NORM_PRIORITY = 5;
public final static int MAX_PRIORITY = 10;
优先级高,有可能先执行,不是一定
*/
thread0.setPriority(10);
thread1.setPriority(1);
thread2.setPriority(1);
thread3.setPriority(1);
thread0.start();
thread1.start();
thread2.start();
thread3.start();
System.out.println(Thread.currentThread().getName());
;
}
}
//线程操作类
class MyThread implements Runnable {
@Override
public void run() {
/*
线程的休眠操作
public static native void sleep(long millis) throws InterruptedException;
*/
for (int i = 0; i < 20; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ",i=" + i);
}
}
}