线程生命周期
线程的生命周期:新建状态、准备状态、运行状态、等待/阻塞状态、死亡状态
示意图:
定义、创建及运行线程
线程:
1 package threadrun; 2 3 //定义一个实现Runnable接口的类 4 class Myrunable1 implements Runnable 5 { 6 public void run() 7 { 8 for(int i=0;i<=50;i++) 9 { 10 System.out.println("线程1"); 11 } 12 } 13 } 14 //定义一个实现Runnable接口的类 15 class Myrunable2 implements Runnable 16 { 17 public void run() 18 { 19 for(int i=0;i<=50;i++) 20 { 21 System.out.println("线程2"); 22 } 23 } 24 } 25 26 public class thread1 { 27 public static void main(String arges[]) 28 { 29 Myrunable1 m1=new Myrunable1(); 30 Myrunable2 m2=new Myrunable2(); 31 Thread t1=new Thread(m1); 32 Thread t2=new Thread(m2); 33 t1.start(); 34 t2.start(); 35 } 36 }
运行结果:
加一个让线程睡眠的方法
1 package threadrun; 2 3 //定义一个实现Runnable接口的类 4 class Myrunable1 implements Runnable 5 { 6 public void run() 7 { 8 for(int i=0;i<=4;i++) 9 { 10 try { 11 Thread.sleep(50);//使用sleep方法使线程进入睡眠状态50毫秒 12 } catch (InterruptedException e) { 13 14 e.printStackTrace(); 15 } 16 System.out.println("线程1"); 17 } 18 } 19 } 20 //定义一个实现Runnable接口的类 21 class Myrunable2 implements Runnable 22 { 23 public void run() 24 { 25 for(int i=0;i<=4;i++) 26 { 27 try { 28 Thread.sleep(50);//使用sleep方法使线程进入睡眠状态50毫秒 29 } catch (InterruptedException e) { 30 31 e.printStackTrace(); 32 } 33 System.out.println("线程2"); 34 } 35 } 36 } 37 38 public class thread1 { 39 public static void main(String arges[]) 40 { 41 Myrunable1 m1=new Myrunable1(); 42 Myrunable2 m2=new Myrunable2(); 43 Thread t1=new Thread(m1); 44 Thread t2=new Thread(m2); 45 t1.start(); 46 t2.start(); 47 } 48 }
运行结果:
设置线程优先级
1 package threadrun; 2 3 //定义一个实现Runnable接口的类 4 class Myrunable1 implements Runnable 5 { 6 public void run() 7 { 8 for(int i=0;i<=4;i++) 9 { 10 System.out.println("线程1"); 11 } 12 } 13 } 14 //定义一个实现Runnable接口的类 15 class Myrunable2 implements Runnable 16 { 17 public void run() 18 { 19 for(int i=0;i<=4;i++) 20 { 21 System.out.println("线程2"); 22 } 23 } 24 } 25 26 public class thread1 { 27 public static void main(String arges[]) 28 { 29 Myrunable1 m1=new Myrunable1(); 30 Myrunable2 m2=new Myrunable2(); 31 Thread t1=new Thread(m1); 32 Thread t2=new Thread(m2); 33 //设置线程优先级 34 t1.setPriority(Thread.MAX_PRIORITY);//最高优先级 35 t2.setPriority(Thread.MAX_PRIORITY);//最低优先级 36 t1.start(); 37 t2.start(); 38 } 39 }
运行结果:
package threadrun; //定义一个实现Runnable接口的类 class Myrunable1 implements Runnable { public void run() { for(int i=0;i<=4;i++) { try { Thread.sleep(50);//使用sleep方法使线程进入睡眠状态50毫秒 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程1"); } } } //定义一个实现Runnable接口的类 class Myrunable2 implements Runnable { public void run() { for(int i=0;i<=4;i++) { try { Thread.sleep(50);//使用sleep方法使线程进入睡眠状态50毫秒 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程2"); } } } public class thread1 { public static void main(String arges[]) { Myrunable1 m1=new Myrunable1(); Myrunable2 m2=new Myrunable2(); Thread t1=new Thread(m1); Thread t2=new Thread(m2); //设置线程优先级 t1.setPriority(Thread.MAX_PRIORITY);//最高优先级 t2.setPriority(Thread.MAX_PRIORITY);//最低优先级 t1.start(); t2.start(); } }
运行结果: