• 多线程的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();
    
    	}
    }
    

      

  • 相关阅读:
    win7下安装memcached
    Integer自动装拆箱
    XShell配色方案
    XShell上传和下载
    基于注解的Spring AOP拦截含有泛型的DAO
    PAT Advance 1020
    PAT Advance 1014
    JavaWeb中文乱码问题解决思路
    eclipse启动错误
    操作系统(一)
  • 原文地址:https://www.cnblogs.com/WINDZLY/p/11788629.html
Copyright © 2020-2023  润新知