1,线程的命名及取得
class MyThreadimplements Runnable{ @Override public void run(){ System.out.println(Thread.currentThread().getName()); //线程命名的取得 } } public class TestDemo{ public static void main(String[] args){ MyThread mt = new MyThread(); new Thread(mt,"线程A").start(); //线程的命名 new Thread(mt,"线程B").start(); new Thread(mt,"线程C").start();
} } |
2、线程的休眠
class MyThreadimplements Runnable{ @Override public void run(){ for(int i = 0;i < 50;i++){ try { Thread.sleep(100); //线程休眠 } catch(InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName()); //线程命名的取得 } } public classTestDemo { public static void main(String[] args){ MyThread mt = new MyThread(); new Thread(mt,"线程A").start(); //线程的命名 new Thread(mt,"线程B").start(); new Thread(mt,"线程C").start();
} } |
3、线程的优先级
取得优先级的时候都是利用一个int整型数据的操作,三种int整型数据的取值:
最高优先级:int MAX_PRIORITY,10
中等优先级:int NORM_PRIORITY,5
最低优先级:int MIN_PRIORITY,1
class MyThreadimplements Runnable{ @Override public void run(){ for(int i = 0;i < 50;i++){ try { Thread.sleep(100); //线程休眠 } catch(InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName()); //线程命名的取得 } } public classTestDemo { public static void main(String[] args){ MyThread mt = new MyThread(); Thread t1 = newThread(mt,"线程A"); //线程命名 Thread t2 = new Thread(mt,"线程B"); Thread t3 = newThread(mt,"线程C");
t1.setPriority(Thread.MIN_PRIORITY); //线程优先级顺序 t2.setPriority(Thread.MAX_PRIORITY); t3.setPriority(Thread.NORM_PRIORITY);
t1.start(); //启动线程 t2.start(); t3.start(); } } |
原文地址:http://9882931.blog.51cto.com/9872931/1616615