#上课笔记,仅自己看得懂
/*
- 线程之间有某种关系,则必须进行通信
- 生产者和消费者的案例
- 为什么不是生产者直接生产给消费者
- 体现高内聚,低耦合
*/
class ShareSource {//共享资源区
public static int Pnum=0;
public static int Tnum=0;
private String nameString;
private String genderString;
private boolean isEmpty=true;//资源是否为空
/*
*
* 生产者向共享资源存储数据
*
*/
//与生产者的接口
synchronized public void push(String nameString,String genderString) {//生产的时候上锁
try {
while(!isEmpty) {
this.wait();
}
this.nameString=nameString;
this.genderString=genderString;
//System.out.println("线程已装"+Pnum++);
Thread.sleep(10);
isEmpty=false;
//this.notify();//唤醒单个线程
this.notifyAll();//唤醒所有线程
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
synchronized public void popup() {//消费的时候上锁
try {
while(isEmpty) {
this.wait();
}
Thread.sleep(10);
System.out.println(this.nameString+"-"+this.genderString);
System.out.println("线程已吃掉"+Tnum++);
isEmpty=true;
this.notifyAll();
//this.notify();//唤醒单个线程
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Consumer implements Runnable{//消费者
private ShareSource resource=null;
public Consumer(ShareSource resource) {
this.resource=resource;
}
public void run() {
for(int i=0;i<50;i++) {
resource.popup();
}
}
}
class Producer implements Runnable{//生产者
private ShareSource resource=null;//共享资源
public Producer(ShareSource resource) {
this.resource=resource;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<50;i++) {
if(i%2==0) {
resource.push("胖胖", "男"+i);
}else {
resource.push("黑牛", "女"+i);
}
}
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
ShareSource resource=new ShareSource();
new Thread(new Producer(resource)).start();//启动线程
new Thread(new Consumer(resource)).start();//
}
}
/*
* wait和notify存在于object下 ,而不是Thread
*/
-----------------------------------------------------------------分割线------------------------------------------------------------------------
/*
*
* 可以调用Lock机制取带synchronized
* 使用condition接口对象调用object类中的await(),signal,signalAll方法
* 取代Object 类中的wait,notify,notifyAll方法
*
*
*/
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class ShareSource {//共享资源区
public static int Pnum=0;
public static int Tnum=0;
private String nameString;
private String genderString;
private boolean isEmpty=true;//资源是否为空
//使用锁机制
private final Lock lock=new ReentrantLock();
/*
*
* 可以调用Lock机制取带synchronized
* 使用condition接口对象调用object类中的await(),signal,signalAll方法
* 取代Object 类中的wait,notify,notifyAll方法
*
*
*/
//申明一个Condition
private Condition condition= lock.newCondition();
/*
*
* 生产者向共享资源存储数据
*
*/
//与生产者的接口
public void push(String nameString,String genderString) {//生产的时候上锁
lock.lock();
try {
while(!isEmpty) {
condition.await();
}
this.nameString=nameString;
this.genderString=genderString;
//System.out.println("线程已装"+Pnum++);
Thread.sleep(10);
isEmpty=false;
//this.notify();//唤醒单个线程
//this.notifyAll();//唤醒所有线程
condition.signal();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void popup() {//消费的时候上锁
try {
lock.lock();
while(isEmpty) {
condition.await();
}
Thread.sleep(10);
System.out.println(this.nameString+"-"+this.genderString);
System.out.println("线程已吃掉"+Tnum++);
isEmpty=true;
//this.notifyAll();
//this.notify();//唤醒单个线程
condition.signal();//唤醒单个线程
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
class Consumer implements Runnable{//消费者
private ShareSource resource=null;
public Consumer(ShareSource resource) {
this.resource=resource;
}
public void run() {
for(int i=0;i<50;i++) {
resource.popup();
}
}
}
class Producer implements Runnable{//生产者
private ShareSource resource=null;//共享资源
public Producer(ShareSource resource) {
this.resource=resource;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<50;i++) {
if(i%2==0) {
resource.push("胖胖", "男"+i);
}else {
resource.push("黑牛", "女"+i);
}
}
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
ShareSource resource=new ShareSource();
new Thread(new Producer(resource)).start();//启动线程
new Thread(new Consumer(resource)).start();//
}
}