package com.xiaoming.homework04; /** * @ClassNameTest05 * @Description TODO * @Author 守护者 * @Date 2022/4/22 6:36 * @Version 1.0 **/ public class Test05 { public static void main(String[] args) { //这里创建一个对象用来配合synchronized来使用 Object o = new Object(); NumThread numThread = new NumThread(o); CharThread charThread = new CharThread(o); //开启线程 numThread.start(); charThread.start(); } } class NumThread extends Thread { private Object obj; public NumThread(Object obj) { this.obj = obj; } @Override public void run() { //同步代码块 synchronized (obj) { for (int i = 1; i <= 52; i++) { System.out.println(i); obj.notifyAll(); if (i % 2 == 0) { try { obj.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } } class CharThread extends Thread { private Object obj; public CharThread(Object obj) { this.obj = obj; } @Override public void run() { synchronized (obj) { for (char i = 'a'; i <= 'z'; i++) { obj.notify(); System.out.println(i); if(i!='z'){ try { obj.wait();//这里线程是不是停止了的? } catch (InterruptedException e) { e.printStackTrace(); } } } } } }