1、多任务
多任务就是一个东西做多个事件,比如你在边吃饭边玩手机,开车打电话之类的。
2、多线程(Thread)
原来是一条路,慢慢的车多了,道路堵塞,效率极低。
然后把道路变宽了,充分利用道路,让效率变高了。
(1、1.1 )
3、进程(Process)
在操作系统中运行的程序就是进程,比如你的微信,QQ,IDEA,游戏
(1、1.2 )
(1、1.3 )
1.2 Java 线程创建
1、线程创建
(1、1.4 )
1.Thread
package Thread; public class Demon01 extends Thread { //创建线程方式1:继承Thread类,重写run()方法,调用start开启线程 @Override public void run() { //run方法线程体 for (int i = 0; i < 20; i++) { System.out.println("我在写代码"+i); } } //main线程,主线程 public static void main(String[] args) { //创建一个线程对象 Demon01 demon01 = new Demon01(); //调用start方法开启线程 demon01.start(); for (int i = 0; i < 20; i++) { System.out.println("gou"+i); } } }
线程中start()方法是同时进行的,主线程与run()线程一起进行,输出结果要看CPU。
而run()方法则是先进行run()线程,然后再进行主线程
2.Runnable
package Thread; public class Demon02 implements Runnable { //创建线程方式 :实现Runnable接口,重写run(),执行线程需要丢入runnable接口实现,调用start() @Override public void run() { for (int i = 0; i < 200; i++) { System.out.println("gou"+i); } } public static void main(String[] args) { Demon02 demon01 = new Demon02(); new Thread(demon01).start(); for (int i = 0; i < 200; i++) { System.out.println(i); } } }
package Thread; //多个线程同时操作同一个对象 //买火车票 //发现问题,多个线程操作统一资源情况下,线程不安全,数据紊乱。 public class Demon03 implements Runnable { //票数 private int ticketNum = 10; @Override public void run() { while (true) { if (ticketNum<=0){ break; } try { Thread.sleep(400); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"——拿到了第"+ticketNum--+"张票"); } } public static void main(String[] args) { Demon03 demon03 = new Demon03(); new Thread(demon03,"1").start(); new Thread(demon03,"2").start(); new Thread(demon03,"3").start(); } }
3.Callable
1.3 静态代理
package Thread; //1.静态代理模式总结 //真实对象和代理的对象实现统一接口 //代理对象要实现真实对象 //代理对象可以做真实对象很多做不了的事情 //真是对象可以专注做自己的事情 public class Demon04 { public static void main(String[] args) { WeddingCompany weddingCompany = new WeddingCompany(new you()); weddingCompany.HappyMarry(); } } interface Marry{ void HappyMarry(); } class you implements Marry{ @Override public void HappyMarry() { System.out.println("你要结婚了"); } } class WeddingCompany implements Marry{ private Marry target; public WeddingCompany(Marry target) { this.target = target; } @Override public void HappyMarry() { before(); this.target.HappyMarry(); after(); } private void after() { System.out.println("布置现场"); } private void before() { System.out.println("收钱"); } }
1.4 Lamda 表达式
避免匿名内部类定义过多。
其实质属于函数式编程的概念
package Thread; public class Demon05 { public static void main(String[] args) { ILove love = (a) ->{ System.out.println("zhe"+a); }; love.love(25); } } interface ILove{ void love(int a); }
1.5 线程状态
1.线程有五大状态
(1、1.6 )
2.线程方法
(1、1.8 )
3.停止线程
(1、1.9 )
package Thread; public class Demon06 implements Runnable { private boolean flag = true; @Override public void run() { int i = 0; while (flag) { System.out.println("run.....Thread" + i++); } } public void stop(){ this.flag = false; } public static void main(String[] args) { Demon06 demon06 = new Demon06(); new Thread(demon06).start(); for (int i = 0; i < 100; i++) { System.out.println("main"+i); if (i==90){ demon06.stop(); System.out.println("线程停止"); } } } }
4.线程休眠
(1、1.10 )
package Thread; import java.text.SimpleDateFormat; import java.util.Date; public class Sleep2 { public static void main(String[] args) { Sleep2 sleep2 = new Sleep2(); Date statTime = new Date(System.currentTimeMillis()); while (true){ try { Thread.sleep(1000); System.out.println(new SimpleDateFormat("HH:mm:ss").format(statTime)); statTime = new Date(System.currentTimeMillis());//更新事件 } catch (InterruptedException e) { e.printStackTrace(); } } } public void tenDown() throws InterruptedException { int time = 10; while (true){ Thread.sleep(1000); System.out.println(time--); if (time<=0){ break; } } } }
5.线程礼让
(1、1.11 )
package Thread; public class Yield { public static void main(String[] args) { MyYield myYield = new MyYield(); new Thread(myYield,"a").start(); new Thread(myYield,"b").start(); } static class MyYield implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread().getName()+"线程开始"); Thread.yield(); System.out.println(Thread.currentThread().getName()+"线程结束"); } } }
6.线程插队
package Thread; //测试 Join 方法,想象是插队 public class Join implements Runnable{ @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println("线程VIP来了"+i); } } public static void main(String[] args) throws InterruptedException { //启动我们的线程 Join join = new Join(); Thread thread= new Thread(join); thread.start(); //主线程 for (int i = 0; i < 500; i++) { if (i==200){ thread.join(); } System.out.println("main"+i); } } }
线程插队就是主线程和VIP线程一开始同时进行,当主线程运行到条件下要进行插队,那么VIP线程先执行并且执行完之后才能在开始进行主线程的执行。
线程插队非常霸道,不建议使用。
7.观测线程状态
(1、1.12 )
package Thread; //观察测试线程的状态 public class TestState { public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(()->{ for (int i = 0; i < 5; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("///"); }); //观察状态 Thread.State state = thread.getState(); System.out.println(state);//NEW //观察启动后 thread.start(); state = thread.getState(); System.out.println(state);//RUN while (state!= Thread.State.TERMINATED){//只要线程不终止,就一直输出状态 Thread.sleep(100); state = thread.getState();//更新线程 System.out.println(state);//输出状态 } } }
1.6 线程优先级
(1、1.13 )
优先级低只是意味着获得调度的概率低,并不是优先级低就不会被调用了,这都是看CPU调度的。
优先级设定建议在start()调度前
package Thread; //测试线程的优先级 public class TestPriority { public static void main(String[] args) { System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority()); MyPriority myPriority = new MyPriority(); Thread th1 = new Thread(myPriority); Thread th2 = new Thread(myPriority); Thread th3 = new Thread(myPriority); Thread th4 = new Thread(myPriority); Thread th5 = new Thread(myPriority); Thread th6 = new Thread(myPriority); //先设置优先级,在启动 th1.start(); th2.setPriority(1); th2.start(); th3.setPriority(4); th3.start(); th4.setPriority(Thread.MAX_PRIORITY); th4.start(); } static class MyPriority implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority()); } } }
1、守护线程
(1、1.14 )
package Thread; //测试守护线程 //上帝守护你 public class TestDaemon { public static void main(String[] args) { God god = new God(); You you = new You(); Thread thread = new Thread(god); thread.setDaemon(true);//默认是false,表示的是用户线程,正常的线程都是用户线程 thread.start(); new Thread(you).start(); } } //上帝 class God implements Runnable{ @Override public void run() { while (true){ System.out.println("上帝保佑着你"); } } } //你 class You implements Runnable { @Override public void run() { for (int i = 0; i < 5000; i++) { System.out.println("你开心的活着"); } System.out.println("========死了========"); } }
2、线程同步机制
线程同步就是多个线程操作同一资源
并发问题? 同一个对象被多个线程同时操作
队列和锁? 队列就是排队打饭,锁就是上厕所时候把门锁上保证自己的安全
(1、1.15 )
3、同步方法
(1、1.16 )
4、死锁
(1、1.17 )
5、线程通信
(1、1.18 )
(1、1.19 )
(1、1.20 )
1.线程通信解决方式
(1、1.21 )
有点类似于取快递
(1、1.22 )
6、线程池
(1、1.23 )
、