使用多线程机制后,main()方法结束,只是主线程结束,主栈空了但其他栈(线程)还在。
线程类继承Thread(java.lang),重写run()方法,在主线程中调用start()方法开启新线程
public class ThreadTest01{ public static void main(String[] args) { Mythread mythread=new Mythread(); //start()启动一个分支线程,在JVM中开辟一个新的栈空间,启动成功的线程会自动调用run方法 //run方法在分支栈底部,main方法在主栈的底部,main和run是平级的 //若直接调用run方法,还在主栈当中,run不结束后面代码执行不了 mythread.start(); for(int i=0;i<1000;i++){ System.out.println("主线程"+i); System.out.println("当前线程:"+Thread.currentThread().getName()); } } } class Mythread extends Thread{ @Override public void run() { for(int i=0;i<1000;i++){ System.out.println("分支线程"+i); } } }实现java.lang.Runnableji恶口并实现run方法,在主线程中将实现接口的对象封装成一个线程对象,调用start()方法开启线程
public class ThreadTest02 { public static void main(String[] args) { MyRunnable myRunnable=new MyRunnable(); //将对象封装成一个线程对象 Thread t=new Thread(myRunnable); t.start(); for(int i=0;i<1000;i++){ System.out.println("主线程"+i); } } } class MyRunnable implements Runnable{ @Override public void run() { for(int i=0;i<1000;i++){ System.out.println("分支线程"+i); } } }和第二种方法同理
public class ThreadTest03 { public static void main(String[] args) { Thread t=new Thread(new Runnable() { @Override public void run() { for(int i=0;i<1000;i++){ System.out.println("分支线程"+i); } } }); t.start(); for(int i=0;i<1000;i++){ System.out.println("主线程"+i); } } }优先级:1-10,默认的优先级是5 设置优先级:setPriority 获取优先级:getPriority
Thread t=new Thread(myRunnable); t.getPriority();//5 t.setPriority(7);线程让位方法,暂停当前的线程,让出cpu,执行其他线程,直到再次分配到时间片 线程状态:运行—>就绪
for(int i=0;i<1000;i++){ system.out.println(i); if(i==499){ Thread.yield(); } }当输出到499,就会暂停这个线程不会继续输出500,转去执行其他线程,当再次分配到时间片后,再回来继续执行,输出500
t.join(),当前线程进入阻塞,cpu让给t线程,t开始线程执行,直到t线程执行结束当前线程才重新唤醒继续执行 线程状态:运行—>阻塞
Thread t=new Thread(myRunnable); t.join();线程状态:运行---->阻塞
唤醒睡眠中的线程,t.interrupt();这种方式依靠异常来唤醒,使用后跳到catch异常中,再执行后续代码 //唤醒睡眠中的线程 public class SleepInterrupt { public static void main(String[] args) { Thread t=new Thread(new MyThread2()); t.setName("m"); t.start(); try { Thread.sleep(1000*5); } catch (InterruptedException e) { e.printStackTrace(); } //这种方式依靠异常来唤醒,使用后跳到catch异常中,在执行后续代码 t.interrupt(); } } class MyThread2 implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName()+"-------->begin"); try { Thread.sleep(1000*60*60*24); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"-------->end"); } }输出结果
m-------->begin java.lang.InterruptedException: sleep interrupted at java.base/java.lang.Thread.sleep(Native Method) at MyThread2.run(SleepInterrupt.java:24) at java.base/java.lang.Thread.run(Thread.java:830) m-------->endstop(): t.stop(); 在主线程中,调用t.stop()方法就可以结束t线程,但是这种方法t线程中未保存的数据就会丢失
设置一个标志变量,通过改变标志变量来结束线程
public class StopThreadTest { public static void main(String[] args) { MyThread03 t=new MyThread03(); Thread th=new Thread(t); th.start(); try { Thread.sleep(4000); } catch (InterruptedException e) { e.printStackTrace(); } //若调用stop方法,线程中未保存的变量会丢失,这里为保存的变量在return前自己写 t.run=false; } } class MyThread03 implements Runnable{ boolean run=true; @Override public void run() { for(int i=0;i<10;i++){ if (run){ System.out.println(i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }else { return; } } } }JDK8以后的新特性,线程类实现Callable接口,这种方法的线程可以获取返回值,因为有时候需要线程结束的时候返回一个值
//带返回值的线程实现方式 public class CallableThreadTest { public static void main(String[] args) { //创建一个“未来任务类”对象 //参数需要给一个Callable接口实现类对象 FutureTask<Integer> task=new FutureTask<>(new Callable() { //call()方法相当于run方法,有返回值 @Override public Object call() throws Exception { Thread.sleep(3000); return 300; } }); Thread t=new Thread(task); t.start(); //获取结果 try { //main线程会受阻,要等待另一个线程的结果 Object obj=task.get(); System.out.println((Integer)obj); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }线程被设置守护线程后,所有其他用户线程结束后,守护线程自动结束
//设置守护线程,所有用户线程结束后,守护线程自动结束 public class DeamonThreadTest { public static void main(String[] args) { Thread t=new BakDataThrea(); //将t设置为守护线程 t.setDaemon(true); t.start(); for(int i=0;i<10;i++){ System.out.println("main:"+i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } class BakDataThrea extends Thread{ @Override public void run() { int i=0; while(true){ System.out.println("守护线程:"+i++); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }