回顾生产者/消费者问题下产生的java多线程(二)
2011-08-24 12:31
477 查看
http://blog.csdn.net/evane1890/article/details/3313772
JAVA语言提供了独立于平台的线程机制,保持了”write once, run anywhere”的特色。同时也提供了对同步机制的良好支持。 在JAVA中,一共有四种方法支持同步,其中三个是同步方法,一个是管道方法。1. 方法wait()/notify()import java.util.LinkedList;
public class Sycn1{
private LinkedList<Object> myList =new LinkedList<Object>();
private int MAX = 10;
public Sycn1(){
}
public void start(){
new Producer().start();
new Consumer().start();
}
public static void main(String[] args) throws Exception{
Sycn1 s1 = new Sycn1();
s1.start();
}
class Producer extends Thread{
public void run(){
while(true){
synchronized(myList){
try{
while(myList.size() == MAX){
System.out.println("warning: it's full!");
myList.wait();
}
Object o = new Object();
if(myList.add(o)){
System.out.println("Producer: " + o);
myList.notify();
}
}catch(InterruptedException ie){
System.out.println("producer is interrupted!");
}
}
}
}
}
class Consumer extends Thread{
public void run(){
while(true){
synchronized(myList){
try{
while(myList.size() == 0){
System.out.println("warning: it's empty!");
myList.wait();
}
Object o = myList.removeLast();
System.out.println("Consumer: " + o);
myList.notify();
}catch(InterruptedException ie){
System.out.println("consumer is interrupted!");
}
}
}
}
}
}
2. 方法await()/signal()在JDK5.0以后,JAVA提供了新的更加健壮的线程处理机制,包括 了同步、锁定、线程池等等,它们可以实现更小粒度上的控制。await()和signal()就是其中用来做同步的两种方法,它们的功能基本上和 wait()/notify()相同,完全可以取代它们,但是它们和新引入的锁定机制Lock直接挂钩,具有更大的灵活性。import java.util.LinkedList;
import java.util.concurrent.locks.*;
public class Sycn2{
private LinkedList<Object> myList = new LinkedList<Object>();
private int MAX = 10;
private final Lock lock = new ReentrantLock();
private final Condition full = lock.newCondition();
private final Condition empty = lock.newCondition();
public Sycn2(){
}
public void start(){
new Producer().start();
new Consumer().start();
}
public static void main(String[] args) throws Exception{
Sycn2 s2 = new Sycn2();
s2.start();
}
class Producer extends Thread{
public void run(){
while(true){
lock.lock();
try{
while(myList.size() == MAX){
System.out.println("warning: it's full!");
full.await();
}
Object o = new Object();
if(myList.add(o)){
System.out.println("Producer: " + o);
empty.signal();
}
}catch(InterruptedException ie){
System.out.println("producer is interrupted!");
}finally{
lock.unlock();
}
}
}
}
class Consumer extends Thread{
public void run(){
while(true){
lock.lock();
try{
while(myList.size() == 0){
System.out.println("warning: it's empty!");
empty.await();
}
Object o = myList.removeLast();
System.out.println("Consumer: " + o);
full.signal();
}catch(InterruptedException ie){
System.out.println("consumer is interrupted!");
}finally{
lock.unlock();
}
}
}
}
}
3. 阻塞队列方法BlockingQueueBlockingQueue也是JDK5.0的一部分,它是一个已经在内部实现了同步的队列,实现方式采用的是我们的第2种await()/signal()方法。它可以在生成对象时指定容量大小。它用于阻塞操作的是put()和take()方法。put()方法类似于我们上面的生产者线程,容量最大时,自动阻塞。take()方法类似于我们上面的消费者线程,容量为0时,自动阻塞。import java.util.concurrent.*;
public class Sycn3{
private LinkedBlockingQueue<Object> queue = new LinkedBlockingQueue<Object>(10);
private int MAX = 10;
public Sycn3(){
}
public void start(){
new Producer().start();
new Consumer().start();
}
public static void main(String[] args) throws Exception{
Sycn3 s3 = new Sycn3();
s3.start();
}
class Producer extends Thread{
public void run(){
while(true){
//synchronized(this){
try{
if(queue.size() == MAX)
System.out.println("warning: it's full!");
Object o = new Object();
queue.put(o);
System.out.println("Producer: " + o);
}catch(InterruptedException e){
System.out.println("producer is interrupted!");
}
//}
}
}
}
class Consumer extends Thread{
public void run(){
while(true){
//synchronized(this){
try{
if(queue.size() == 0)
System.out.println("warning: it's empty!");
Object o = queue.take();
System.out.println("Consumer: " + o);
}catch(InterruptedException e){
System.out.println("producer is interrupted!");
}
//}
}
}
}
}
4. 管道方法PipedInputStream/PipedOutputStream
JAVA语言提供了独立于平台的线程机制,保持了”write once, run anywhere”的特色。同时也提供了对同步机制的良好支持。 在JAVA中,一共有四种方法支持同步,其中三个是同步方法,一个是管道方法。1. 方法wait()/notify()import java.util.LinkedList;
public class Sycn1{
private LinkedList<Object> myList =new LinkedList<Object>();
private int MAX = 10;
public Sycn1(){
}
public void start(){
new Producer().start();
new Consumer().start();
}
public static void main(String[] args) throws Exception{
Sycn1 s1 = new Sycn1();
s1.start();
}
class Producer extends Thread{
public void run(){
while(true){
synchronized(myList){
try{
while(myList.size() == MAX){
System.out.println("warning: it's full!");
myList.wait();
}
Object o = new Object();
if(myList.add(o)){
System.out.println("Producer: " + o);
myList.notify();
}
}catch(InterruptedException ie){
System.out.println("producer is interrupted!");
}
}
}
}
}
class Consumer extends Thread{
public void run(){
while(true){
synchronized(myList){
try{
while(myList.size() == 0){
System.out.println("warning: it's empty!");
myList.wait();
}
Object o = myList.removeLast();
System.out.println("Consumer: " + o);
myList.notify();
}catch(InterruptedException ie){
System.out.println("consumer is interrupted!");
}
}
}
}
}
}
2. 方法await()/signal()在JDK5.0以后,JAVA提供了新的更加健壮的线程处理机制,包括 了同步、锁定、线程池等等,它们可以实现更小粒度上的控制。await()和signal()就是其中用来做同步的两种方法,它们的功能基本上和 wait()/notify()相同,完全可以取代它们,但是它们和新引入的锁定机制Lock直接挂钩,具有更大的灵活性。import java.util.LinkedList;
import java.util.concurrent.locks.*;
public class Sycn2{
private LinkedList<Object> myList = new LinkedList<Object>();
private int MAX = 10;
private final Lock lock = new ReentrantLock();
private final Condition full = lock.newCondition();
private final Condition empty = lock.newCondition();
public Sycn2(){
}
public void start(){
new Producer().start();
new Consumer().start();
}
public static void main(String[] args) throws Exception{
Sycn2 s2 = new Sycn2();
s2.start();
}
class Producer extends Thread{
public void run(){
while(true){
lock.lock();
try{
while(myList.size() == MAX){
System.out.println("warning: it's full!");
full.await();
}
Object o = new Object();
if(myList.add(o)){
System.out.println("Producer: " + o);
empty.signal();
}
}catch(InterruptedException ie){
System.out.println("producer is interrupted!");
}finally{
lock.unlock();
}
}
}
}
class Consumer extends Thread{
public void run(){
while(true){
lock.lock();
try{
while(myList.size() == 0){
System.out.println("warning: it's empty!");
empty.await();
}
Object o = myList.removeLast();
System.out.println("Consumer: " + o);
full.signal();
}catch(InterruptedException ie){
System.out.println("consumer is interrupted!");
}finally{
lock.unlock();
}
}
}
}
}
3. 阻塞队列方法BlockingQueueBlockingQueue也是JDK5.0的一部分,它是一个已经在内部实现了同步的队列,实现方式采用的是我们的第2种await()/signal()方法。它可以在生成对象时指定容量大小。它用于阻塞操作的是put()和take()方法。put()方法类似于我们上面的生产者线程,容量最大时,自动阻塞。take()方法类似于我们上面的消费者线程,容量为0时,自动阻塞。import java.util.concurrent.*;
public class Sycn3{
private LinkedBlockingQueue<Object> queue = new LinkedBlockingQueue<Object>(10);
private int MAX = 10;
public Sycn3(){
}
public void start(){
new Producer().start();
new Consumer().start();
}
public static void main(String[] args) throws Exception{
Sycn3 s3 = new Sycn3();
s3.start();
}
class Producer extends Thread{
public void run(){
while(true){
//synchronized(this){
try{
if(queue.size() == MAX)
System.out.println("warning: it's full!");
Object o = new Object();
queue.put(o);
System.out.println("Producer: " + o);
}catch(InterruptedException e){
System.out.println("producer is interrupted!");
}
//}
}
}
}
class Consumer extends Thread{
public void run(){
while(true){
//synchronized(this){
try{
if(queue.size() == 0)
System.out.println("warning: it's empty!");
Object o = queue.take();
System.out.println("Consumer: " + o);
}catch(InterruptedException e){
System.out.println("producer is interrupted!");
}
//}
}
}
}
}
4. 管道方法PipedInputStream/PipedOutputStream
相关文章推荐
- 回顾生产者/消费者问题下产生的java多线程(一)
- java 多线程学习之多生产者多消费者产生的线程安全问题分析与解决:Lock和Condition
- 黑马程序员-java多线程生产者消费者问题
- 生产者与消费者问题(java多线程实现)
- 继续java多线程——生产者消费者类似问题
- 【java 多线程】多线程并发同步问题及生产者、消费者问题
- Java多线程的一个案例-生产者消费者问题
- java多线程之生产者消费者经典问题
- java多线程之生产者消费者经典问题
- JAVA基础再回首(二十五)——Lock锁的使用、死锁问题、多线程生产者和消费者、线程池、匿名内部类使用多线程、定时器、面试题
- 由生产者/消费者问题看JAVA多线程
- JAVA学习第二十七课(多线程(六))- 多生产者多消费者问题(JDK1.5新特性)
- java多线程模拟生产者消费者问题,公司面试常常问的题。。。
- java多线程之生产者消费者经典问题
- Java 多线程 生产者与消费者问题测试代码
- Java多线程之生产者/消费者问题
- 由生产者/消费者问题看JAVA多线程
- 22.用JAVA中的多线程示例生产者和消费者问题
- 多线程经典问题之生产者消费者问题的JAVA实现
- Java 多线程实现生产者消费者问题(wait/notify)