码迷,mamicode.com
首页 > 编程语言 > 详细

多线程的wait notify方法

时间:2019-11-03 20:27:54      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:oid   cti   big   art   write   false   tput   兔子   cat   

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();

	}
}

  

多线程的wait notify方法

标签:oid   cti   big   art   write   false   tput   兔子   cat   

原文地址:https://www.cnblogs.com/WINDZLY/p/11788629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!