public enum State { NEW, //新建状态,创建了线程对象 RUNNABLE, //(睡眠完成之后||等待事件消除转为就绪)调用start方法,就绪状态 就绪状态或者了cpu使用权,转为运行状态(枚举未有) ;失去cpu执行权,或者调用了 yield()方法 转为 就绪状态 BLOCKED, //阻塞状态,同步等待锁对象池 获得锁对象转为就绪状态 WAITING, //等待状态 1)sleep() 2)join() 3)wait() TIMED_WAITING, //等待状态 TERMINATED; //终止状态 1)run方法运行结束 2)调用stop方法 }
/*线程休眠:Thread.sleep(2000) *1)静态方法,通过类名调用 * 2)sleep()方法所在的线程睡眠 * 3)2000为毫秒 * */ public class ThreadSleep { public static void main(String[] args) { Thread t1 = new Thread(new Runnable() { @Override public void run() { for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+i); //当i为20,睡眠5秒 if(i==20){ try { Thread.sleep(5000); //run()定义没有抛出异常,重写也不能抛出异常 } catch (InterruptedException e) { e.printStackTrace(); } } } } }); t1.start(); } }
//线程中断 public class ThreadInterrupt { public static void main(String[] args) { Thread t1 = new Thread(new Runnable() { @Override public void run() { for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+i); //当i为20,睡眠5秒 if(i==20){ try { Thread.sleep(5000); //run()定义没有抛出异常,重写也不能抛出异常 //如果当前线程睡眠中断会产生异常 } catch (InterruptedException e) { e.printStackTrace(); } } } } }); t1.start(); //main线程 for(int i=1;i<70;i++){ System.out.println(Thread.currentThread().getName()+"----<"+i); } //当main线程结束,将t1唤醒 t1.interrupt(); } }
public class ThreadYield { public static void main(String[] args) { Thread t1 = new Thread(new Runnable() { @Override public void run() { for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+1); if(i%10==0){ Thread.yield(); //线程让步,当前线程会释放cpu执行权,转为就绪状态 } } } }); t1.start(); Thread t2 = new Thread(new Runnable() { @Override public void run() { for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+"---->"+1); } } }); t2.start(); } }
public class ThreadJoin { public static void main(String[] args) { Thread t1 = new Thread(new Runnable() { @Override public void run() { for(int i=1;i<50;i++){ System.out.println(Thread.currentThread().getName()+i); } } }); t1.start(); Thread t2 = new Thread(new Runnable() { @Override public void run() { for(int i=1;i<50;i++){ System.out.println(Thread.currentThread().getName()+i); if(i==20){ try { t1.join();//当i=20,加入t1线程,当前线程转为等待状态,等t1线程执行完之后,当前线程转为就绪状态 } catch (InterruptedException e) { e.printStackTrace(); } } } } }); t2.start(); } }
Thread t1 = new Thread(new Runnable() { @Override public void run() { for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+i); } } }); t1.start(); for(int i=0;i<20;i++){ System.out.println(Thread.currentThread().getName()+"---->"+i); } t1.stop();//main方法结束 将t1线程终止
public class ThreadStop extends Thread { boolean running = true;//线程运行标志 public void run() { for (int i = 0; running & i < 300; i++) { System.out.println(Thread.currentThread().getName() + "---" + i); } } } class test10 { public static void main(String[] args) { ThreadStop t1 = new ThreadStop(); t1.start(); for (int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName() + "main----" + i); } t1.running = false;//修改子线程的标志 } }
public class ThreadStop1 implements Runnable{ boolean stopping = false; @Override public void run() { for(int i=1;i<300;i++){ if(stopping){ return;//结束run方法执行 } System.out.println(Thread.currentThread().getName()+"----"+i); } } } class test11{ public static void main(String[] args) { ThreadStop1 t2=new ThreadStop1(); new Thread(t2).start(); //main线程 for(int i=1;i<=100;i++){ System.out.println(Thread.currentThread().getName()+i); } t2.stopping=true;//修改实现类 的标志 } }