ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Java 线程同步问题 生产者-消费者 算法实现

Java 线程同步问题 生产者-消费者 算法实现

原创 Linux操作系统 作者:clusty 时间:2009-07-16 14:08:05 0 删除 编辑
/* 消费者生产者线程演示
 * Coding by ZhaoHongliang(赵洪亮).
查看原文
**/
public class ProducerConsumer {
    
public static void main(String[] args) {
        Container container 
= new Container();
        Producer Alice 
= new Producer("Alice",container);
        Thread produceThread1 
= new Thread(Alice);
        produceThread1.start();
        Producer Bob 
= new Producer("Bob",container);
        Thread produceThread2 
= new Thread(Bob);
        produceThread2.start();
        Producer Lucy 
= new Producer("Lucy",container);
        Thread produceThread3 
= new Thread(Lucy);
        produceThread3.start();
        Consumer James 
= new Consumer("James",container);
        Thread consumeThread1 
= new Thread(James);
        consumeThread1.start();
        Consumer Charlie 
= new Consumer("Charlie",container);
        Thread consumeThread2 
= new Thread(Charlie);
        consumeThread2.start();
    }

}

/* 生产者 */
class Producer implements Runnable {
    
private String name;
    
private Container container = null;
    
//Constructor
    public Producer(String producerName,Container container) {
        
this.name = producerName;
        
this.container = container;
    }

    
//Override abstract methoed of Interface Runnable.
    public void run() {
        
while(true{
            
synchronized(this.container) {
                
while(!this.container.isEmpty()) {
                    
try {
                        
this.container.wait();
                    }

                    
catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

                Product pushedProduct 
= new Product(this.name);
                container.push(pushedProduct);
                
try {
                    Thread.sleep((
int)(Math.random()*2000));
                }

                
catch (InterruptedException e) {
                    e.printStackTrace();
                }

                
this.container.notifyAll();
            }

        }

    }

}

/* 消费者 */
class Consumer implements Runnable {
    
private String name;
    
private Container container = null;
    
//Constructor
    public Consumer(String consumerName,Container container) {
        
this.name = consumerName;
        
this.container = container;
    }

    
//Override abstract methoed of Interface Runnable.
    public void run() {
        
while(true{
            
synchronized (this.container) {
                
while(this.container.isEmpty()) {
                    
try {
                        
this.container.wait();
                    }

                    
catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

                Product popedProduct 
= container.pop(name);
                
try {
                    Thread.sleep((
int)(Math.random()*1000));
                }

                
catch (InterruptedException e) {
                    e.printStackTrace();
                }

                
this.container.notifyAll();
            }

        }

    }

}

/* 产品 */
class Product {
    
private static int id = 0;
    
//Constructor
    private String producerName;
    
public Product(String producerName) {
        
this.producerName = producerName;
        
this.id ++;
    }

    
//Override method of Class Object.
    public String toString() {
        
return "Product No."+id+"; Made by "+producerName;
    }

}

/* 容器 只允许放一个产品 */
class Container {
    
private Product container = null;
    
//Test if the container is empty.
    public boolean isEmpty() {
        
if (this.container==null{
            
return true;
        }

        
else {
            
return false;
        }

    }

    
public synchronized void push(Product product) {
        
if (this.isEmpty()) {
            
this.container = product;
            System.out.println(
"Pushed: "+product);
        }

        
else {
            
throw new RuntimeException("Can't push! The container is full.");
        }

    }

    
public synchronized Product pop(String consumerName) {
        
if (!this.isEmpty()) {
            Product temp 
= this.container;
            
this.container = null;
            System.out.println(consumerName
+" poped: "+temp);
            
return temp;
        }

        
else {
            
throw new RuntimeException("Can't pop! The container is empty.");
        }

    }
 
}
/**
运行结果(死循环)
Pushed: Product No.1; Made by Alice
Charlie poped: Product No.1; Made by Alice
Pushed: Product No.2; Made by Lucy
James poped: Product No.2; Made by Lucy
Pushed: Product No.3; Made by Alice
Charlie poped: Product No.3; Made by Alice
Pushed: Product No.4; Made by Bob
James poped: Product No.4; Made by Bob
Pushed: Product No.5; Made by Alice
Charlie poped: Product No.5; Made by Alice
Pushed: Product No.6; Made by Lucy
James poped: Product No.6; Made by Lucy
Pushed: Product No.7; Made by Alice
Charlie poped: Product No.7; Made by Alice
Pushed: Product No.8; Made by Bob
James poped: Product No.8; Made by Bob
Pushed: Product No.9; Made by Alice
Charlie poped: Product No.9; Made by Alice
Pushed: Product No.10; Made by Lucy
James poped: Product No.10; Made by Lucy
。。。 。。。
*/

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/21468125/viewspace-609322/,如需转载,请注明出处,否则将追究法律责任。

上一篇: Java Annotation入门
下一篇: Java技术网址收藏
请登录后发表评论 登录
全部评论

注册时间:2009-07-16

  • 博文量
    9
  • 访问量
    8804