Process 进程
Thread 线程
概念
程序运行起来,产生一个进程,进程执行起来则执行进程中的线程
/*** @Author: haifengzuishuai* @Data: 2021/6/4 4:56 下午*/public class Hello extends Thread{@Overridepublic void run() {for (int i = 0; i < 20; i++) {System.out.println("bbbb"+i);}}public static void main(String[] args) {Hello hello = new Hello();hello.start();//调用start同时运行for (int i = 0; i < 3; i++) {System.out.println("aaaa"+i);}}}
创建多线程实例
需导入commons-io-2.6.jar包
import org.apache.commons.io.FileUtils;import java.io.File;import java.io.IOException;import java.net.URL;/*** @Author: haifengzuishuai* @Data: 2021/6/6 8:42 下午*/public class A extends Thread {private String url;private String name;public A(String url, String name) {this.url = url;this.name = name;}@Overridepublic void run() {WebDownloader webDownloader = new WebDownloader();webDownloader.downloader(url, name);sout(name)}public static void main(String[] args) {A a = new A("https://i1.hdslb.com/bfs/face/83bb511365da513c55aa3d1958524f3b7db40684.jpg@96w_96h_1c.webp", "1.jpg");A b = new A("https://i1.hdslb.com/bfs/face/83bb511365da513c55aa3d1958524f3b7db40684.jpg@96w_96h_1c.webp", "2.jpg");A c = new A("https://i1.hdslb.com/bfs/face/83bb511365da513c55aa3d1958524f3b7db40684.jpg@96w_96h_1c.webp", "3.jpg");A d = new A("https://i1.hdslb.com/bfs/face/83bb511365da513c55aa3d1958524f3b7db40684.jpg@96w_96h_1c.webp", "4.jpg");a.start();b.start();c.start();d.start();}}class WebDownloader {public void downloader(String url, String name) {try {FileUtils.copyURLToFile(new URL(url), new File(name));} catch (IOException e) {e.printStackTrace();}}}
龟兔赛跑
/*** @Author: haifengzuishuai* @Data: 2021/6/7 11:32 下午*/public class C implements Runnable {private static String winner;@Overridepublic void run() {for (int i = 0; i <= 100; i++) {if (Thread.currentThread().getName().equals("王八")) {System.out.println("休眠了");try {Thread.sleep(2);//毫秒} catch (InterruptedException e) {e.printStackTrace();}}boolean flag = gameOver(i);if (flag) {break;}System.out.println(Thread.currentThread().getName() + "--<跑了" + i + "米");}}public boolean gameOver(int steps) {if (winner != null) {return true;}{if (steps >= 100) {winner = Thread.currentThread().getName();System.out.println("winner is " + winner);return true;}}return false;}public static void main(String[] args) {C c = new C();new Thread(c, "兔子").start();new Thread(c, "王八").start();}}
lamda表达式
- 定义一个函数式接口
- 必须是函数式接口,函数式接口只有一个方法
a = (params)=>{sout....}ILove love = null;love = a->{sout.... 代码块}love.love(params)//函数式接口interface ILove{void love(int a)}
线程
状态
- 新建Thread t = new Theread()
- 就绪t.start()
- 运行 cpu开始执行
- 阻塞sleep wait 或同步锁定
- ded 线程销毁
线程方法
线程停止flag
通过标记位的方式停止
/*** @Author: haifengzuishuai* @Data: 2021/6/8 10:29 下午*/public class TestStop implements Runnable {private boolean flag = true;@Overridepublic void run() {int i = 0;while (flag) {System.out.println("while " + i++);}}public void stop() {this.flag = false;}public static void main(String[] args) {TestStop testStop = new TestStop();new Thread(testStop).start();for (int i = 0; i < 1000; i++) {System.out.println("main =>" + i);if (i == 900) {testStop.stop();System.out.println("xianchengtingzhile");}}}}
线程休眠sleep
sleep
线程礼让yield
yield
线程插队join
/*** @Author: haifengzuishuai* @Data: 2021/6/8 10:53 下午*/public class TestYield implements Runnable {@Overridepublic void run() {for (int i = 0; i < 500; i++) {System.out.println("Vip Throd-->" + i);}}public static void main(String[] args) throws InterruptedException {TestYield testYield = new TestYield();Thread thread = new Thread(testYield);thread.start();for (int i = 0; i < 500; i++) {if (i == 200) {thread.join();//插队}System.out.println("main==>" + i);}}}
线程状态state
死亡之后的线程不可再次start
/*** @Author: haifengzuishuai* @Data: 2021/6/9 3:34 下午*/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("aaaaafor");});//观察状态Thread.State state = thread.getState();System.out.println(state);//NEW//观察状态启动后thread.start();Thread.State state1 = thread.getState();System.out.println(state1);//RUNNABLEwhile (state != Thread.State.TERMINATED) {Thread.sleep(1000);state = thread.getState();//updateSystem.out.println(state);//TERMINATED}}}
线程优先级priority
/*** @Author: haifengzuishuai* @Data: 2021/6/9 3:48 下午*/public class TestPriority {public static void main(String[] args) {System.out.println(Thread.currentThread().getName()+"main-->"+Thread.currentThread().getPriority());Mypriority mypriority = new Mypriority();Thread thread1 = new Thread(mypriority,"aaa");Thread thread2 = new Thread(mypriority,"bbb");Thread thread3 = new Thread(mypriority,"ccc");Thread thread4 = new Thread(mypriority,"ddd");Thread thread5 = new Thread(mypriority,"eee");thread1.setPriority(5);thread5.setPriority(10);thread1.start();thread2.start();thread3.start();thread4.start();thread5.start();}}class Mypriority implements Runnable{@Overridepublic void run() {System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());}}
守护线程daemon
守护线程会守护未被守护的线程结束后,守护线程再结束
/*** @Author: haifengzuishuai* @Data: 2021/6/9 4:07 下午*/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);Thread thread1 = new Thread(you);thread.start();thread1.start();}}class God implements Runnable{@Overridepublic void run() {while (true){System.out.println("god");}}}class You implements Runnable{@Overridepublic void run() {for (int i = 0; i < 10000; i++) {System.out.println("aaaa"+i);}}}
并发synchronized
多线程同时访问一个对象
每个对象都有一把锁
降低性能
性能倒置
package syn;/*** @Author: haifengzuishuai* @Data: 2021/6/9 8:27 下午*/public class UnsafeBuyp {public static void main(String[] args) {BuyTicket buyTicket = new BuyTicket();new Thread(buyTicket, "aaa").start();new Thread(buyTicket, "bbb").start();new Thread(buyTicket, "ccc").start();}}class BuyTicket implements Runnable {private int ticketNums = 5;boolean flag = true;@Overridepublic void run() {while (flag) {try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}try {buy();} catch (InterruptedException e) {e.printStackTrace();}}}//synchronized同步方法private synchronized void buy() throws InterruptedException {if (ticketNums <= 0) {flag = false;return;}System.out.println(Thread.currentThread().getName() + "拿到了" + ticketNums--);}}
死锁
Lock
