publicclassThreadTest{publicstaticvoidmain(String[] args){finalObject o =newObject();getThread('a','h', o).start();getThread('A','H', o).start();}publicstaticThreadgetThread(char from,charto,Object o){returnnewThread(()->{synchronized(o){for(char i = from; i <=to; i++){System.err.println(Thread.currentThread().getName()+" : "+ i);
o.notify();try{//本线程停止,释放锁
o.wait();}catch(InterruptedException e){
e.printStackTrace();}}}});}}
1.1效果
Thread-0: a
Thread-1:AThread-0: b
Thread-1:BThread-0: c
Thread-1:CThread-0: d
Thread-1:DThread-0: e
Thread-1:EThread-0: f
Thread-1:FThread-0: g
Thread-1:GThread-0: h
Thread-1:H
1.简单实现public class ThreadTest { public static void main(String[] args) { final Object o = new Object(); new Thread(new Number(o)).start(); new Thread(new UpperAlpha(o)).start(); }}class Number implements Runnable { O