Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1994705
  • 博文数量: 606
  • 博客积分: 9991
  • 博客等级: 中将
  • 技术积分: 5725
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-17 19:07
文章分类

全部博文(606)

文章存档

2011年(10)

2010年(67)

2009年(155)

2008年(386)

分类:

2009-02-16 17:51:36

实例一:

public class ProducerConsumer {
    public static void main(String args[]){
    SyncStack stack = new SyncStack();
    Runnable p=new Producer(stack);
    Runnable c = new Consumer(stack);
    Thread t1 = new Thread(p);
    Thread t2 = new Thread(c);
    t1.start();
    t2.start();
    }
}


class SyncStack{ //支持多线程同步操作的堆栈的实现
private int index = 0;
private char []data = new char[6];
public synchronized void push(char c){
   if(index == data.length){
   try{
     this.wait();
   }catch(InterruptedException e){}
   }
   this.notify();
   data[index] = c;
   index++;
}
public synchronized char pop(){
   if(index ==0){
    try{
     this.wait();
    }catch(InterruptedException e){}
   }
   this.notify();
   index--;
   return data[index];
}
}


class Producer implements Runnable{
SyncStack stack;
public Producer(SyncStack s){
   stack = s;
}
public void run(){
   for(int i=0; i<20; i++){
    char c =(char)(Math.random()*26+'A');
    stack.push(c);
    System.out.println("produced:"+c);
    try{              
     Thread.sleep((int)(Math.random()*1000));
    }catch(InterruptedException e){
    }
   }
}
}


class Consumer implements Runnable{
SyncStack stack;
public Consumer(SyncStack s){
   stack = s;
}
public void run(){
   for(int i=0;i<20;i++){
    char c = stack.pop();
    System.out.println("消费:"+c);
    try{                 
     Thread.sleep((int)(Math.random()*1000));
    }catch(InterruptedException e){
    }
   }
}
}

实例二:notify()是随机唤醒一个线程

如果生产者生产好东西,然后一直随机唤醒的线程都是生产者那就产生死锁了,改用notifyAll()则可以避免死锁,但是效率会降低,不过总比死锁来的好得多。

程序代码:

common仓库类

package threadlocktest;

/**
*
*
@author DJ尐舞
*/
public class common {

private char ch;
private boolean available = false;

synchronized char get() {
while (available == false) {
try {
wait();
}
catch (InterruptedException e) {
}
}
available
= false;
notifyAll();
return ch;
}

synchronized void put(char newch) {
while (available == true) {
try {
wait();
}
catch (InterruptedException e) {
}
}
ch
=newch;
available
= true;
notifyAll();
}
}

 

消费者:

package threadlocktest;

/**
*
*
@author DJ尐舞
*/
public class consumer extends Thread{
private common comm;
public consumer(common thiscomm){
this.comm=thiscomm;
}
public void run(){
char c;
for(int i=0;i<5;i++){
c
=comm.get();
System.out.println(
"消费者得到的数据是:"+c);
}
}

}
 
生产者:
package threadlocktest;

/**
*
*
@author DJ尐舞
*/
public class producer extends Thread {

private common comm;

public producer(common thiscomm) {
comm
= thiscomm;
}
public void run(){
char c;
for(c='a';c<='e';c++){

comm.put(c); System.out.println(
"生产者者的数据是:"+c);
}
}
}
 
main函数
Code
package threadlocktest;

/**
*
*
@author DJ尐舞
*/
public class Main {

/**
*
@param args the command line arguments
*/
public static void main(String[] args) {
common comm
= new common();
for (int i = 0; i < 100; i++) {
new producer(comm).start();
new consumer(comm).start();
}
}
}
阅读(1448) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~