zoukankan      html  css  js  c++  java
  • 多线程的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();
    
    	}
    }
    

      

  • 相关阅读:
    一定间隔时间下重复执行一个函数的几个方法
    关于 MonoDevelop on Linux 单步调试问题的解决
    MonoDevelop 4.2.2/Mono 3.4.0 in CentOS 6.5 安装笔记
    在ASP.NET MVC 4 on Mono中使用OracleClient in CentOS 6.x的问题记录
    在CentOS 6.4 x86_32中使用Rhythmbox听MP3
    MonoDevelop 4.0.9 on CentOS 6.3 安装笔记
    MemoryMappedFile 在 Mono in Linux 的开发笔记
    Mono on CentOS 6.3 安装笔记
    庆祝下:iOS 开发者企业级计划(299美元/年帐户+邓白氏码免费) 和 Windows Phone公司应用(公司帐户99美元+Symantec企业证书299美元/年))顺利发布成功
    PowerDesigner中NAME和COMMENT的互相转换,需要执行语句
  • 原文地址:https://www.cnblogs.com/WINDZLY/p/11788629.html
Copyright © 2011-2022 走看看