image.png
image.png

image.pngimage.png

死锁:

  1. //死锁
  2. public class Synchronized {
  3. public static void main(String[] args) {
  4. MyThread thread = new MyThread();
  5. MyThread thread1 = new MyThread();
  6. thread.start();
  7. thread1.start();
  8. }
  9. }
  10. class MyThread extends Thread {
  11. static Object object1 = new Object();
  12. static Object object2 = new Object();
  13. @Override
  14. public void run() {
  15. while (true){
  16. test01();
  17. test02();
  18. }
  19. }
  20. public static void test01(){
  21. synchronized(object1){
  22. System.out.println("1");
  23. synchronized(object2){
  24. System.out.println("2");
  25. }
  26. }
  27. }
  28. public static void test02(){
  29. synchronized(object2){
  30. System.out.println("3");
  31. synchronized(object1){
  32. System.out.println("4");
  33. }
  34. }
  35. }
  36. }

死锁原理:
image.png

生产消费者线程

  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. //生产消费者线程
  5. public class Synchronized {
  6. public static void main(String[] args) {
  7. //运输媒介
  8. List<Integer> storage = new ArrayList<>(100);
  9. new Thread(new Product(storage)).start();
  10. new Thread(new Consumer(storage)).start();
  11. }
  12. }
  13. class Product implements Runnable {
  14. private List<Integer> storage;
  15. public Product(List<Integer> storage) {
  16. this.storage = storage;
  17. }
  18. @Override
  19. public void run() {
  20. while (true) {
  21. synchronized (storage) {
  22. if (storage.size() < 10) {
  23. storage.add(1);
  24. System.out.println("生产一个");
  25. } else {
  26. try {
  27. //唤醒所有在使用storage对象的线程
  28. storage.notifyAll();
  29. //让出当前使用storage对象并抢占了锁的线程,总之就是让出当前线程占用的锁
  30. storage.wait();
  31. } catch (InterruptedException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }
  36. }
  37. }
  38. }
  39. class Consumer implements Runnable {
  40. List<Integer> storage;
  41. public Consumer(List<Integer> storage) {
  42. this.storage = storage;
  43. }
  44. @Override
  45. public void run() {
  46. while (true) {
  47. synchronized (storage) {
  48. if (storage.size() > 0) {
  49. Iterator it = storage.iterator();
  50. while (it.hasNext()){
  51. System.out.println("消费:"+it.next());
  52. it.remove();
  53. }
  54. }else {
  55. try {
  56. storage.notifyAll();
  57. storage.wait();
  58. } catch (InterruptedException e) {
  59. e.printStackTrace();
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }

例:奇数偶数交换输出

  1. import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. public class HomeWork {
  5. public static void main(String[] args) {
  6. List<Integer> list = new ArrayList<>(1);
  7. Integer i = 1;
  8. list.add(i);
  9. new Thread(new Even(list)).start();
  10. new Thread(new Odd(list)).start();
  11. }
  12. }
  13. class Even implements Runnable {
  14. private List<Integer> list;
  15. public Even(List<Integer> list) {
  16. this.list = list;
  17. }
  18. @Override
  19. public void run() {
  20. while (true) {
  21. try {
  22. Thread.sleep(1000);
  23. } catch (InterruptedException e) {
  24. e.printStackTrace();
  25. }
  26. synchronized (list) {
  27. if (list.get(0) % 2 != 0) {
  28. System.out.println("奇数:"+list.get(0));
  29. list.set(0,list.get(0)+1);
  30. }else {
  31. list.notifyAll();
  32. try {
  33. list.wait();
  34. } catch (InterruptedException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }
  39. }
  40. }
  41. }
  42. class Odd implements Runnable {
  43. private List<Integer> list;
  44. public Odd(List<Integer> list) {
  45. this.list = list;
  46. }
  47. @Override
  48. public void run() {
  49. while (true) {
  50. synchronized (list) {
  51. try {
  52. Thread.sleep(1000);
  53. } catch (InterruptedException e) {
  54. e.printStackTrace();
  55. }
  56. if (list.get(0) % 2 == 0) {
  57. System.out.println("偶数:"+list.get(0));
  58. list.set(0,list.get(0)+1);
  59. }else {
  60. list.notifyAll();
  61. try {
  62. list.wait();
  63. } catch (InterruptedException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }