线程交替输入,打印
package com.thread; /** * @author a * @date 2021/3/16 7:58 * @description */ public class Test4 { static Thread t1 = null, t2 = null; public static void main(String[] args) { char[] number = "123456789".toCharArray(); char[] letter = "ABCDEFGHI".toCharArray(); Object o = new Object(); t1 = new Thread(new Runnable() { @Override public void run() { synchronized (o) { for (char c : number) { System.out.println(c); try { o.notify(); o.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } o.notify(); } /*for (char c : number) { System.out.println(c); LockSupport.unpark(t2); LockSupport.park(); }*/ } }, "t1"); t2 = new Thread(new Runnable() { @Override public void run() { synchronized (o) { for (char c : letter) { System.out.println(c); try { o.notify(); o.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } o.notify(); } /*for (char c : letter) { LockSupport.park(); System.out.println(c); LockSupport.unpark(t1); }*/ } }, "t2"); t1.start(); t2.start(); System.out.println("====================="); } }