生产者消费者问题(附代码)与线程池配置

分析:
1.在库存为满时,生产者才可以生产,同理有库存时消费者才可以消费,否则就等待。
2.当消费者消费完,活着生产者生产完  应该去通知对方,并释放对象锁
3.对象的wait方法,wait方法的作用是释放当前线程的所获得的锁,
4.notifyAll() 方法, 通知(唤醒)该对象上其他等待线程,使得其继续执行。

5.synchronized关键字只作用与当前实例对象的方法。

 

package com.producer.consumer;

public class MainTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        /**
         * 初始化数据池和一个生产线程A 两个消费线程D E
         * 
         */
		BufferData data = new BufferData();
		new Thread(new Producer(data, "A")).start();
		// new Thread(new Producer(data,"B")).start();
		// new Thread(new Producer(data,"C")).start();
		new Thread(new Consumer(data, "D")).start();
		new Thread(new Consumer(data, "E")).start();
	}

}

运行log
生产者

 

package com.producer.consumer;

public class Producer implements Runnable {
	public String mThreadName;
	public BufferData mData;

	public Producer(BufferData data, String threadName) {
		this.mThreadName = threadName;
		this.mData = data;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 10; i++) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//随机生产存入 数据池
			int random = (int) (Math.random() * 1000 + 1);
			mData.toProducer(random);
			System.out.println(mThreadName + "线程存入:" + random);
		}

	}

}

 


消费者

package com.producer.consumer;

public class Consumer implements Runnable {
	public String mThreadName;
	public BufferData mBufferData;

	public Consumer(BufferData data, String threadName) {
		this.mBufferData = data;
		this.mThreadName = threadName;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 10; i++) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int value = mBufferData.toConsumer();
			System.out.println(mThreadName + "线程消费:" + value);

		}
	}

}

 

线程基础知识点

import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
 * 线程池配置参数说明
 * 第一个参数核心线程数 并发的
 * 第二个参数 非核心线程数=核心线程数+缓存队列Runnable数 
 * 第三个参数 非核心线程超时时间
 * 第四个参数 时间单位
 * 第五个参数缓存队列
 *    ArrayBlockingQueue 大小固定的 顺序执行
 *    LinkedBlockingQueue 无限制的 顺序执行
 *    PriorityBlockingQueue 优先级执行
 *    SynchronizedQueue 同步执行 并发 不缓存
 * 第六个线程工厂
 * @author zwh
 *
 */
public class LocalThreadPool {
	private static LocalThreadPool mInstance;
	private ThreadPoolExecutor threadPool;
	private static int MAX_CORE_NUMBER = Runtime.getRuntime().availableProcessors();
	private LocalThreadPool() {
		threadPool = new ThreadPoolExecutor(MAX_CORE_NUMBER, 10, 60, TimeUnit.SECONDS,
				new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {
					AtomicInteger auto = new AtomicInteger(0);
					@Override
					public Thread newThread(Runnable r) {
						// TODO Auto-generated method stub
						Thread thread = new Thread(r, "crate-thread-" + auto.incrementAndGet());
						System.out.println("create thread:" + thread.getName());
						return thread;
					}

				});
		threadPool.allowCoreThreadTimeOut(true);
	}

	public static LocalThreadPool getInstance() {
		if (mInstance == null) {
			synchronized (LocalThreadPool.class) {
				if (mInstance == null) {
					mInstance = new LocalThreadPool();
				}
			}

		}
		return mInstance;
	}

	public Future<?> submit(Runnable runnable) {
		return threadPool.submit(runnable);
	}

	public int count() {
		return threadPool.getActiveCount();
	}

	public void destory() {
		threadPool.shutdown();
	}

}

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章