线程的命名与取得
- 构造方法:public Thread(Runnable target , String name);
- 设置名字:public final void setName(String name);
- 取得名字:public final String getName();
范例:
package cn.cccc.demo;class MyThread implements Runnable{ public void run (){ System.out.println(Thread.currentThread().getName()); }}public class ThreadDemo { public static void main(String[] args) throws Exception { MyThread mt = new MyThread(); new Thread(mt,"线程A").start(); new Thread(mt).start(); new Thread(mt,"线程B").start(); }}//线程A2 collapsed lines
//Thread-0//线程B线程的休眠 休眠:public static void sleep(long millis ) throws InterruptedException; 休眠:public static void sleep(long millis, int nanos ) throws InterruptedException; 在进行休眠的时候可能会产生中断异常“InterruptedException”,中断异常属于Exception的子类必须处理。 范例:
package cn.cccc.demo;public class ThreadDemo { public static void main(String[] args) throws Exception { new Thread(()->{ for (int x= 0 ; x <10; x++){ System.out.println(Thread.currentThread().getName()+"、x = " + x); try { Thread.sleep(1000); //暂缓执行 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } },"线程对象").start(); }1 collapsed line
}线程中断 在之前发现线程休眠提供有一个中断异常,实际上就证明线程的休眠是可以被打断的。 在Thread类里面有提供这种中断执行的处理方法: 判断线程是否被中断:public boolean isInterrupteed(); 中断线程执行:public void interrupt();
范例:
package cn.cccc.demo;public class ThreadDemo { public static void main(String[] args) throws Exception { Thread thread = new Thread(()->{ System.out.println("我要睡觉了"); try { Thread.sleep(3000); System.out.println("睡够了"); } catch (Exception e) { // TODO Auto-generated catch block System.out.println("敢打扰我睡觉,老子宰了你"); } //准备睡十秒
}); thread.start();10 collapsed lines
Thread.sleep(1000); if(!thread.isInterrupted()){ System.out.println("打扰一下你睡觉"); thread.interrupt(); } }}//我要睡觉了//打扰一下你睡觉//敢打扰我睡觉,老子宰了你所有执行的线程都是可以被中断的,中断线程必须进行异常的处理。
线程的强制执行: 所为的线程的强制执行指的是当满足某些条件之后,某一个线程对象将可以一直独占资源,一直到该线程的程序执行结束。 范例:观察一个没有强制执行的程序
package cn.cccc.demo;public class ThreadDemo { public static void main(String[] args) throws Exception { Thread thread = new Thread(()->{ for (int x= 0; x < 100; x++) { System.out.println(Thread.currentThread().getName() + " 执行、 x = " + x); } },"玩耍的线程"); thread.start(); for(int x = 0; x < 100; x++){ System.out.println("[霸道的main线程]number = " + x); } }}这个时候主线程和子线程都在交替执行,一直抢占资源。如果希望主程序独占执行,那么就可以利用: 强制执行:public final void join() throws InterruptedException;
package cn.cccc.demo;public class ThreadDemo { public static void main(String[] args) throws Exception { Thread mainThread = Thread.currentThread(); //获得主线程 Thread thread = new Thread(()->{ for (int x= 0; x < 100; x++) { if(x ==3 ){ // 如果执行到等于3 了,则霸道线程加入 try { mainThread.join(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + " 执行、 x = " + x);8 collapsed lines
} },"玩耍的线程"); thread.start(); for(int x = 0; x < 100; x++){ System.out.println("[霸道的main线程]number = " + x); } }}线程的礼让:线程的礼让指的是先将资源让出去让别的线程先执行。线程的礼让可以使用Thread中提供的方法: 礼让:public static void yield(); 范例:礼让操作
package cn.cccc.demo;public class ThreadDemo { public static void main(String[] args) throws Exception { Thread mainThread = Thread.currentThread(); //获得主线程 Thread thread = new Thread(()->{
for (int x= 0; x < 100; x++) { if(x %3 ==0 ){ Thread.yield(); //线程礼让 System.out.println("####线程的礼让操作####"); } System.out.println(Thread.currentThread().getName() + " 执行、 x = " + x); } },"玩耍的线程"); thread.start();5 collapsed lines
for(int x = 0; x < 100; x++){ System.out.println("[霸道的main线程]number = " + x); } }}每次调用yeild()方法只会礼让一次。
线程的优先级:从理论上来讲,线程的优先级越高就越有可能先执行。在Thread类里面针对优先级操作有两个处理方法 设置优先级
final void setPriority(int newPriority) 获取优先级 final int getPriority() 在进行优先级定义的时候都是通过int型的数字来完成的,而对于此数字的选择在Thread类里面就定义有三个常量: 最高优先级:public static final int MAX_PRIORITY;(10) 中等优先级:public static final int NORM_PRIORITY;(5) 最低优先级:public static final int MIN_PRIORITY;(1)package cn.cccc.demo;public class ThreadDemo { public static void main(String[] args) throws Exception { Runnable run = ()->{ for(int x= 0; x < 5; x ++){ try { Thread.sleep(1000); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"执行"); } }; Thread threadA = new Thread(run , "线程对象A");11 collapsed lines
Thread threadB = new Thread(run , "线程对象B"); Thread threadC = new Thread(run , "线程对象C"); threadA.setPriority(1); threadB.setPriority(5); threadC.setPriority(10); threadA.start(); threadB.start(); threadC.start();
}}优先级高的有可能限制性,而不是绝对先执行