/**
* 线程间通讯
* 其实就是多个线程在操作同一个资源
* 但是操作的动作不同
*
* 等待与唤醒机制
* 等待的线程放在线程池
* 将代码进一步优化
*/
package java160109;
/**
* @author LiZheng
*
*/
public class InputOutputDemo {
/**
* @param args
*/
public static void main(String[] args) {
Res res = new Res();
// Input input = new Input(res);
// Output output = new Output(res);
// Thread thread1 = new Thread(input);
// Thread thread2 = new Thread(output);
// thread1.start();
// thread2.start();
new Thread(new Input(res)).start();
new Thread(new Output(res)).start();
}
}
class Res {
private String name;
private String sex;
private boolean flag = false;
public synchronized void set(String name, String sex) {
if (flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name = name;
this.sex = sex;
flag = true;
this.notify();
}
public synchronized void out() {
if (!flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name + "......." + sex);
flag = false;
this.notify();
}
}
class Input implements Runnable {
private Res res = new Res();
// Object object =new Object();
public Input(Res res) {
this.res = res;
}
@Override
public void run() {
int x = 0;
while (true) {
// synchronized (res) {
// if (res.flag) {
// try {
// res.wait();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
if (x == 0) {
// res.name = "mike";
// res.sex = "man";
res.set("mike", "man");
} else {
// res.name = "Helen";
// res.sex = "women";
res.set("Helen", "women");
}
x = (x + 1) % 2;
// res.notify();
// }
}
}
}
/**
* wait() notity() notifyAll() 都是用在同步中,因为要对持有监视器(缩)的线程操作 所以要使用在同步中,只有同步才具有锁
* 为什么这些操作方法在操作同步线程时,都必须标识他们所操作的线程
*
* @author Administrator
*
*/
class Output implements Runnable {
private Res res;
// Object object =new Object();
Output(Res res) {
this.res = res;
}
@Override
public void run() {
while (true) {
// synchronized (res) {
// if (!res.flag) {
// try {
// res.wait();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// System.out.println(res.name + "..." + res.sex);
//
// res.flag = false;
// res.notify();
// }
res.out();
}
}
}
* 线程间通讯
* 其实就是多个线程在操作同一个资源
* 但是操作的动作不同
*
* 等待与唤醒机制
* 等待的线程放在线程池
* 将代码进一步优化
*/
package java160109;
/**
* @author LiZheng
*
*/
public class InputOutputDemo {
/**
* @param args
*/
public static void main(String[] args) {
Res res = new Res();
// Input input = new Input(res);
// Output output = new Output(res);
// Thread thread1 = new Thread(input);
// Thread thread2 = new Thread(output);
// thread1.start();
// thread2.start();
new Thread(new Input(res)).start();
new Thread(new Output(res)).start();
}
}
class Res {
private String name;
private String sex;
private boolean flag = false;
public synchronized void set(String name, String sex) {
if (flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name = name;
this.sex = sex;
flag = true;
this.notify();
}
public synchronized void out() {
if (!flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name + "......." + sex);
flag = false;
this.notify();
}
}
class Input implements Runnable {
private Res res = new Res();
// Object object =new Object();
public Input(Res res) {
this.res = res;
}
@Override
public void run() {
int x = 0;
while (true) {
// synchronized (res) {
// if (res.flag) {
// try {
// res.wait();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
if (x == 0) {
// res.name = "mike";
// res.sex = "man";
res.set("mike", "man");
} else {
// res.name = "Helen";
// res.sex = "women";
res.set("Helen", "women");
}
x = (x + 1) % 2;
// res.notify();
// }
}
}
}
/**
* wait() notity() notifyAll() 都是用在同步中,因为要对持有监视器(缩)的线程操作 所以要使用在同步中,只有同步才具有锁
* 为什么这些操作方法在操作同步线程时,都必须标识他们所操作的线程
*
* @author Administrator
*
*/
class Output implements Runnable {
private Res res;
// Object object =new Object();
Output(Res res) {
this.res = res;
}
@Override
public void run() {
while (true) {
// synchronized (res) {
// if (!res.flag) {
// try {
// res.wait();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// System.out.println(res.name + "..." + res.sex);
//
// res.flag = false;
// res.notify();
// }
res.out();
}
}
}