public class Res {
public String name;
public String sex;
public boolean flag=false;
}
public class Inputments implements Runnable {
private Res res;
public Inputments(Res res){
this.res=res;
}
@Override
public void run() {
int x=0;
int y=0;
while(y<1000){
synchronized (res) {
if(res.flag){
try {
res.wait(); //wait() notify() notifyAll() 都要使用在同步中,因为要对持有监视器(锁)的线程操作
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(x==0){
res.name="wang"+y;
res.sex="nan";
}else{
res.name="张青"+y;
res.sex="女";
}
res.flag=true;
res.notify();
}
System.out.println(Thread.currentThread().getName()+res.name+","+res.sex);
x=(x+1)%2;
y++;
}
}
}
public class OutMents implements Runnable {
private Res res;
public OutMents(Res res){
this.res=res;
}
@Override
public void run() {
int y=0;
while(y<1000){
synchronized (res) {
if(!res.flag){
try {
res.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+res.name + "," + res.sex);
res.flag=false;
res.notify();
}
y++;
}
}
}
Res res=new Res();
Inputments inputments = new Inputments(res);
OutMents outMents = new OutMents(res);
Thread th1=new Thread(inputments);
Thread th2=new Thread(outMents);
th1.start();
th2.start();
多线程通信优化代码:
//资源
public class Resource {
private String name;
private String sex;
private boolean flag =false;
public synchronized void getName() {
if(!flag)
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("姓名:"+name+",性别:"+sex);
flag=false;
this.notify();
}
public synchronized void setName(String name,String sex) {
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name = name+Thread.currentThread().getName();
this.sex =sex;
flag = true;
this.notify();
}
}
//输入
public class Input implements Runnable {
private Resource res;
public Input(Resource res) {
this.res = res;
}
@Override
public void run() {
int i=0;
while(true){
if(i==0){
res.setName("张山", "男");
}else
res.setName("luce", "woman");
i =(i+1) %2;
}
}
}
//输出
package com.thread;
public class Output implements Runnable {
private Resource res;
public Output(Resource res) {
this.res = res;
}
@Override
public void run() {
int i=0;
while(i<100){
res.getName();
i++;
}
}
public static void main(String[] args) {
Resource res = new Resource();
new Thread(new Input(res)).start();
new Thread(new Output(res)).start();
}
}
生产者和消费者:
对于多个生产者和消费者。
为什么要定义while判读标记
原因:被唤醒的线程再判断一次标记
为什么要定义notifyAll
因为需要唤醒对方线程
因为用notif容易唤醒本方线程,导致所有的线程都等待。
//生产者和消费者:
public class ProductionResource {
private String name;
private int count =1;
private boolean flag =false;
public synchronized void setName(String name) {
while(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name = name+(count++);
System.out.println(Thread.currentThread().getName()+"__生产者__名称:"+this.name);
flag = true;
this.notifyAll();
}
public synchronized void getName() {
while(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"__消费者___名称:"+name);
flag=false;
this.notifyAll();
}
}
//生产者:
public class ProductionResource {
private String name;
private int count =1;
private boolean flag =false;
public synchronized void setName(String name) {
while(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name = name+(count++);
System.out.println(Thread.currentThread().getName()+"__生产者__名称:"+this.name);
flag = true;
this.notifyAll();
}
public synchronized void getName() {
while(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"__消费者___名称:"+name);
flag=false;
this.notifyAll();
}
}
//消费者:
public class Customer implements Runnable {
private ProductionResource res;
public Customer(ProductionResource res) {
this.res = res;
}
@Override
public void run() {
while(true){
res.getName();
}
}
public static void main(String[] args) {
ProductionResource res = new ProductionResource();
new Thread(new Production(res)).start();
new Thread(new Production(res)).start();
new Thread(new Customer(res)).start();
new Thread(new Customer(res)).start();
}
}
用1.5新技术解决生产和消费问题:
//资源 用Lock 接口 和condition
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ProductionResource2 {
private String name;
private int count =1;
private boolean flag =false;
private Lock lock =new ReentrantLock();
private Condition conditionPro = lock.newCondition();
private Condition conditionCus = lock.newCondition();
public void setName(String name) {
lock.lock();
try {
while(flag){
conditionPro.await();
}
this.name = name+(count++);
System.out.println(Thread.currentThread().getName()+"__生产者__名称:"+this.name);
flag = true;
conditionCus.signal();
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
lock.unlock();
}
}
public void getName() {
lock.lock();
try {
while(!flag){
conditionCus.await();
}
System.out.println(Thread.currentThread().getName()+"__消费者___名称:"+name);
flag=false;
conditionPro.signal();
} catch (InterruptedException e) {
e.printStackTrace();
}
finally{
lock.unlock();
}
}
}
//生产
public class Production implements Runnable {
private ProductionResource2 res;
public Production(ProductionResource2 res) {
this.res = res;
}
@Override
public void run() {
while(true){
res.setName("洗衣服");
}
}
}
//消费
public class Customer implements Runnable {
private ProductionResource2 res;
public Customer(ProductionResource2 res) {
this.res = res;
}
@Override
public void run() {
while(true){
res.getName();
}
}
public static void main(String[] args) {
ProductionResource2 res = new ProductionResource2();
new Thread(new Production(res)).start();
new Thread(new Production(res)).start();
new Thread(new Customer(res)).start();
new Thread(new Customer(res)).start();
}
}