Process 进程

Thread 线程

概念

程序运行起来,产生一个进程,进程执行起来则执行进程中的线程

  1. /**
  2. * @Author: haifengzuishuai
  3. * @Data: 2021/6/4 4:56 下午
  4. */
  5. public class Hello extends Thread{
  6. @Override
  7. public void run() {
  8. for (int i = 0; i < 20; i++) {
  9. System.out.println("bbbb"+i);
  10. }
  11. }
  12. public static void main(String[] args) {
  13. Hello hello = new Hello();
  14. hello.start();//调用start同时运行
  15. for (int i = 0; i < 3; i++) {
  16. System.out.println("aaaa"+i);
  17. }
  18. }
  19. }

创建多线程实例

需导入commons-io-2.6.jar包

  1. import org.apache.commons.io.FileUtils;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.net.URL;
  5. /**
  6. * @Author: haifengzuishuai
  7. * @Data: 2021/6/6 8:42 下午
  8. */
  9. public class A extends Thread {
  10. private String url;
  11. private String name;
  12. public A(String url, String name) {
  13. this.url = url;
  14. this.name = name;
  15. }
  16. @Override
  17. public void run() {
  18. WebDownloader webDownloader = new WebDownloader();
  19. webDownloader.downloader(url, name);
  20. sout(name)
  21. }
  22. public static void main(String[] args) {
  23. A a = new A("https://i1.hdslb.com/bfs/face/83bb511365da513c55aa3d1958524f3b7db40684.jpg@96w_96h_1c.webp", "1.jpg");
  24. A b = new A("https://i1.hdslb.com/bfs/face/83bb511365da513c55aa3d1958524f3b7db40684.jpg@96w_96h_1c.webp", "2.jpg");
  25. A c = new A("https://i1.hdslb.com/bfs/face/83bb511365da513c55aa3d1958524f3b7db40684.jpg@96w_96h_1c.webp", "3.jpg");
  26. A d = new A("https://i1.hdslb.com/bfs/face/83bb511365da513c55aa3d1958524f3b7db40684.jpg@96w_96h_1c.webp", "4.jpg");
  27. a.start();
  28. b.start();
  29. c.start();
  30. d.start();
  31. }
  32. }
  33. class WebDownloader {
  34. public void downloader(String url, String name) {
  35. try {
  36. FileUtils.copyURLToFile(new URL(url), new File(name));
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. }

龟兔赛跑

  1. /**
  2. * @Author: haifengzuishuai
  3. * @Data: 2021/6/7 11:32 下午
  4. */
  5. public class C implements Runnable {
  6. private static String winner;
  7. @Override
  8. public void run() {
  9. for (int i = 0; i <= 100; i++) {
  10. if (Thread.currentThread().getName().equals("王八")) {
  11. System.out.println("休眠了");
  12. try {
  13. Thread.sleep(2);//毫秒
  14. } catch (InterruptedException e) {
  15. e.printStackTrace();
  16. }
  17. }
  18. boolean flag = gameOver(i);
  19. if (flag) {
  20. break;
  21. }
  22. System.out.println(Thread.currentThread().getName() + "--<跑了" + i + "米");
  23. }
  24. }
  25. public boolean gameOver(int steps) {
  26. if (winner != null) {
  27. return true;
  28. }
  29. {
  30. if (steps >= 100) {
  31. winner = Thread.currentThread().getName();
  32. System.out.println("winner is " + winner);
  33. return true;
  34. }
  35. }
  36. return false;
  37. }
  38. public static void main(String[] args) {
  39. C c = new C();
  40. new Thread(c, "兔子").start();
  41. new Thread(c, "王八").start();
  42. }
  43. }

lamda表达式

  1. 定义一个函数式接口
  2. 必须是函数式接口,函数式接口只有一个方法
  1. a = (params)=>{
  2. sout....
  3. }
  4. ILove love = null;
  5. love = a->{
  6. sout.... 代码块
  7. }
  8. love.love(params)
  9. //函数式接口
  10. interface ILove{
  11. void love(int a)
  12. }

线程

状态

  1. 新建Thread t = new Theread()
  2. 就绪t.start()
  3. 运行 cpu开始执行
  4. 阻塞sleep wait 或同步锁定
  5. ded 线程销毁

线程方法

线程停止flag

通过标记位的方式停止

  1. /**
  2. * @Author: haifengzuishuai
  3. * @Data: 2021/6/8 10:29 下午
  4. */
  5. public class TestStop implements Runnable {
  6. private boolean flag = true;
  7. @Override
  8. public void run() {
  9. int i = 0;
  10. while (flag) {
  11. System.out.println("while " + i++);
  12. }
  13. }
  14. public void stop() {
  15. this.flag = false;
  16. }
  17. public static void main(String[] args) {
  18. TestStop testStop = new TestStop();
  19. new Thread(testStop).start();
  20. for (int i = 0; i < 1000; i++) {
  21. System.out.println("main =>" + i);
  22. if (i == 900) {
  23. testStop.stop();
  24. System.out.println("xianchengtingzhile");
  25. }
  26. }
  27. }
  28. }

线程休眠sleep

sleep

线程礼让yield

yield

线程插队join

  1. /**
  2. * @Author: haifengzuishuai
  3. * @Data: 2021/6/8 10:53 下午
  4. */
  5. public class TestYield implements Runnable {
  6. @Override
  7. public void run() {
  8. for (int i = 0; i < 500; i++) {
  9. System.out.println("Vip Throd-->" + i);
  10. }
  11. }
  12. public static void main(String[] args) throws InterruptedException {
  13. TestYield testYield = new TestYield();
  14. Thread thread = new Thread(testYield);
  15. thread.start();
  16. for (int i = 0; i < 500; i++) {
  17. if (i == 200) {
  18. thread.join();//插队
  19. }
  20. System.out.println("main==>" + i);
  21. }
  22. }
  23. }

线程状态state

死亡之后的线程不可再次start

  1. /**
  2. * @Author: haifengzuishuai
  3. * @Data: 2021/6/9 3:34 下午
  4. */
  5. public class TestState {
  6. public static void main(String[] args) throws InterruptedException {
  7. Thread thread = new Thread(() -> {
  8. for (int i = 0; i < 5; i++) {
  9. try {
  10. Thread.sleep(1000);
  11. } catch (InterruptedException e) {
  12. e.printStackTrace();
  13. }
  14. }
  15. System.out.println("aaaaafor");
  16. });
  17. //观察状态
  18. Thread.State state = thread.getState();
  19. System.out.println(state);//NEW
  20. //观察状态启动后
  21. thread.start();
  22. Thread.State state1 = thread.getState();
  23. System.out.println(state1);//RUNNABLE
  24. while (state != Thread.State.TERMINATED) {
  25. Thread.sleep(1000);
  26. state = thread.getState();//update
  27. System.out.println(state);//TERMINATED
  28. }
  29. }
  30. }

线程优先级priority

  1. /**
  2. * @Author: haifengzuishuai
  3. * @Data: 2021/6/9 3:48 下午
  4. */
  5. public class TestPriority {
  6. public static void main(String[] args) {
  7. System.out.println(Thread.currentThread().getName()+"main-->"+Thread.currentThread().getPriority());
  8. Mypriority mypriority = new Mypriority();
  9. Thread thread1 = new Thread(mypriority,"aaa");
  10. Thread thread2 = new Thread(mypriority,"bbb");
  11. Thread thread3 = new Thread(mypriority,"ccc");
  12. Thread thread4 = new Thread(mypriority,"ddd");
  13. Thread thread5 = new Thread(mypriority,"eee");
  14. thread1.setPriority(5);
  15. thread5.setPriority(10);
  16. thread1.start();
  17. thread2.start();
  18. thread3.start();
  19. thread4.start();
  20. thread5.start();
  21. }
  22. }
  23. class Mypriority implements Runnable{
  24. @Override
  25. public void run() {
  26. System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
  27. }
  28. }

守护线程daemon

守护线程会守护未被守护的线程结束后,守护线程再结束

  1. /**
  2. * @Author: haifengzuishuai
  3. * @Data: 2021/6/9 4:07 下午
  4. */
  5. public class TestDaemon {
  6. public static void main(String[] args) {
  7. God god = new God();
  8. You you = new You();
  9. Thread thread = new Thread(god);
  10. // thread.setDaemon(true);
  11. Thread thread1 = new Thread(you);
  12. thread.start();
  13. thread1.start();
  14. }
  15. }
  16. class God implements Runnable{
  17. @Override
  18. public void run() {
  19. while (true){
  20. System.out.println("god");
  21. }
  22. }
  23. }
  24. class You implements Runnable{
  25. @Override
  26. public void run() {
  27. for (int i = 0; i < 10000; i++) {
  28. System.out.println("aaaa"+i);
  29. }
  30. }
  31. }

并发synchronized

多线程同时访问一个对象

每个对象都有一把锁

降低性能

性能倒置

  1. package syn;
  2. /**
  3. * @Author: haifengzuishuai
  4. * @Data: 2021/6/9 8:27 下午
  5. */
  6. public class UnsafeBuyp {
  7. public static void main(String[] args) {
  8. BuyTicket buyTicket = new BuyTicket();
  9. new Thread(buyTicket, "aaa").start();
  10. new Thread(buyTicket, "bbb").start();
  11. new Thread(buyTicket, "ccc").start();
  12. }
  13. }
  14. class BuyTicket implements Runnable {
  15. private int ticketNums = 5;
  16. boolean flag = true;
  17. @Override
  18. public void run() {
  19. while (flag) {
  20. try {
  21. Thread.sleep(100);
  22. } catch (InterruptedException e) {
  23. e.printStackTrace();
  24. }
  25. try {
  26. buy();
  27. } catch (InterruptedException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }
  32. //synchronized同步方法
  33. private synchronized void buy() throws InterruptedException {
  34. if (ticketNums <= 0) {
  35. flag = false;
  36. return;
  37. }
  38. System.out.println(Thread.currentThread().getName() + "拿到了" + ticketNums--);
  39. }
  40. }

死锁

  1. Lock

线程写作