多线程的wait notify方法
package test; import java.awt.List; import java.awt.image.AreaAveragingScaleFilter; import java.lang.reflect.Array; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; import java.io.BufferedInputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Reader; import java.io.Writer; import javax.security.auth.callback.LanguageCallback; import javax.swing.text.StyledEditorKit.BoldAction; class Resource { String name; String sex; boolean flag = false; } class Input implements Runnable { Resource r; Input(Resource r) { this.r = r; } public void run() { int x = 0; while (true) { synchronized (r) { if (r.flag) { try { r.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } if (x == 0) { r.name = "佩琪"; r.sex = "男 ! !"; } else { r.name = "小兔子"; r.sex = "女 ? ?"; } x = x ^ 1; r.flag = true; r.notify(); } } } } class Output implements Runnable { Resource r; Output(Resource r) { this.r = r; } @Override public void run() { while (true) { synchronized (r) { if (!r.flag) { try { r.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(r.name + " " + r.sex); r.flag = true; r.notify(); } } } } public class Main { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(new BufferedInputStream(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); Resource r = new Resource(); Input a = new Input(r); Output b = new Output(r); Thread t1 = new Thread(a); Thread t2 = new Thread(b); t1.start(); t2.start(); } } //优化后的方法 package test; import java.awt.List; import java.awt.image.AreaAveragingScaleFilter; import java.lang.reflect.Array; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; import java.io.BufferedInputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Reader; import java.io.Writer; import javax.security.auth.callback.LanguageCallback; import javax.swing.text.StyledEditorKit.BoldAction; class Resource { private String name; private String sex; private boolean flag = false; public synchronized void set(String name, String sex) { this.name = name; this.sex = sex; if (flag) { try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } flag = true; this.notify(); } public synchronized void ot() { if (!flag) { try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println(name + " " + sex); flag = false; this.notify(); } } class Input implements Runnable { Resource r; Input(Resource r) { this.r = r; } public void run() { int x = 0; while (true) { if (x == 0) { r.set("佩琪", "男 ! !"); } else { r.set("小兔子", "女 ? ?"); } x = x ^ 1; } } } class Output implements Runnable { Resource r; Output(Resource r) { this.r = r; } @Override public void run() { while (true) { r.ot(); } } } public class Main { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(new BufferedInputStream(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); Resource r = new Resource(); Input a = new Input(r); Output b = new Output(r); Thread t1 = new Thread(a); Thread t2 = new Thread(b); t1.start(); t2.start(); } }