多线程
常见创建方式:
1、继承
1 class Mythread extends Thread{ 2 private int i = 0; 3 public void run(){ 4 system.out.print(i) 5 } 6 }
Thread t = new Mythread ();
t.start();
2、实现
1 class MyRunnable implements Runnable{ 2 private int i = 0; 3 public void run(){ 4 system.out.print(i); 5 } 6 }
Runnable muRunnable = new MyRunnable();
Thread t = new Thread(muRunnable,"线程1")
t.start();
3、Lambda表达式
new Thread(() -> System.out.println("线程开启!")).start()
基本状态:
1、新建(new):Thread t = new MyThread();
2、就绪(Runnable):t.start(),等待CPU调用;
3、运行状态(Running):CPU调用,线程进入运行状态;
4、阻塞状态(Blocked):处于暂时放弃CPU使用权,停止执行;
等待阻塞:wait();
同步阻塞:synchronized;
其他阻塞:sleep()、join()
5、死亡状态(Dead):线程执行完run方法;
https://www.runoob.com/java/java-multithreading.html