Java多线程实践

来源:互联网 发布:手机关机定位软件 编辑:程序博客网 时间:2024/05/16 11:16

 

Sun提供的:

/*
 * Producer.java
 *
 * Created on 2008年2月24日, 下午2:11
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 
*/


package threadPC;

/**
 *
 * 
@author rulinma
 
*/

public class Producer extends Thread 
{
    
private CubbyHole cubbyhole;
    
private int number;

    
private Producer()
    
{
    }


    
public Producer(CubbyHole c, int number) 
    
{
        cubbyhole 
= c;
        
this.number = number;
    }


    
public void run() 
    
{
        
for (int i = 0; i < 10; i++
        
{
            System.out.println(
"Producer #" + this.number + " put: " + i);
            cubbyhole.put(i);
               //System.out.println("Producer #" + this.number + " put: " + i);
            
            
try 
            
{
                sleep((
int)(Math.random() * 100));
            }

            
catch (InterruptedException e)
            
{
            }

        }

    }

}


 

 

/*
 * Consumer.java
 *
 * Created on 2008年2月24日, 下午2:16
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 
*/


package threadPC;

/**
 *
 * 
@author rulinma
 
*/

public class Consumer extends Thread  {
    
    
private CubbyHole cubbyhole;
    
private int number;
  
    
/** Creates a new instance of Consumer */
    
public Consumer() {
    }


    
public Consumer(CubbyHole c, int number) 
   
{
        cubbyhole 
= c;
        
this.number = number;
    }


    
public void run() 
   
{
        
int value = 0;
        
for (int i = 0; i < 10; i++)
        
{
            value 
= cubbyhole.get();
            System.out.println(
"Consumer #" + this.number + " got: " + value);
        }

    }


}

 

 

/*
 * CubbyHole.java
 *
 * Created on 2008年2月24日, 下午2:05
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 
*/


package threadPC;

/**
 *
 * 
@author rulinma
 
*/

public class CubbyHole 
{
    
    
private int contents;
    
private boolean available = false;
    
    
public CubbyHole()
    
{
    }

    
    
/*
     * synchronized同步提供单一线程访问
     
*/

    
public synchronized int get() 
    
{
        
//System.out.println("aviaialbe " + available);
        while (available == false
        
{
            
try 
            
{
                
//System.out.println("get waiting...");
                wait();
                
//available = false;
            }
 
            
catch (InterruptedException e) 
            
{
            }

        }

        notifyAll();
        available 
= false;
        
        
//System.out.println("contents:"+contents);
        return contents;
    }


     
public synchronized void put(int value) 
     
{
        
//System.out.println("aviaialbe " + available);
        while (available == true
        
{
            
try 
            
{
                
//System.out.println("put waiting...");
                wait();
                
//available = true;
            }
 
            
catch (InterruptedException e) 
            

            }

        }

        contents 
= value;
        notifyAll();
        available 
= true;
        
//System.out.println("contents:"+contents);
        
    }

}




 

/*
 * ProducerConsumerTest.java
 *
 * Created on 2008年2月24日, 下午2:18
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 
*/


package threadPC;

/**
 *
 * 
@author rulinma
 
*/

public class ProducerConsumerTest {
    
 
    
public ProducerConsumerTest() {
    }

    
    
public static void main(String[] args) 
    
{
          CubbyHole c 
= new CubbyHole();
          
          Producer p1 
= new Producer(c, 1);
          Consumer c1 
= new Consumer(c, 1);

          p1.start();
          c1.start();
    }



}

运行结果:

init:
deps-jar:
compile-single:
run-single:
Producer #1 put: 0
Consumer #1 got: 0
Producer #1 put: 1
Consumer #1 got: 1
Producer #1 put: 2
Consumer #1 got: 2
Producer #1 put: 3
Consumer #1 got: 3
Producer #1 put: 4
Consumer #1 got: 4
Producer #1 put: 5
Consumer #1 got: 5
Producer #1 put: 6
Consumer #1 got: 6
Producer #1 put: 7
Consumer #1 got: 7
Producer #1 put: 8
Consumer #1 got: 8
Producer #1 put: 9
Consumer #1 got: 9
生成成功(总时间:1 秒)

运行过程如果使用上述代码中的红色代码而不是蓝色的 运行结果会有所不同。

 

原创粉丝点击