生產者消費者問題是研究多線程程序時繞不開的問題
解決生產者消費者問題的方法有兩種
同步問題的核心在於
現在同步問題的解決方法一般是采用信號或者加鎖機制
JAVA語言提供了獨立於平台的線程機制
在JAVA中
下面我們看各個方法的實現
wait()和notify()是根類Object的兩個方法
wait()方法表示
notify()方法表示
下面是一個例子代碼
import java
public class Sycn
private LinkedList<Object> myList =new LinkedList<Object>();
private int MAX =
public Sycn
}
public void start(){
new Producer()
new Consumer()
}
public static void main(String[] args) throws Exception{
Sycn
s
}
class Producer extends Thread{
public void run(){
while(true){
synchronized(myList){
try{
while(myList
System
myList
}
Object o = new Object();
if(myList
System
myList
}
}catch(InterruptedException ie){
System
}
}
}
}
}
class Consumer extends Thread{
public void run(){
while(true){
synchronized(myList){
try{
while(myList
System
myList
}
Object o = myList
System
myList
}catch(InterruptedException ie){
System
}
}
}
}
}
}
在JDK
下面是一個例子代碼
import java
import ncurrent
public class Sycn
private LinkedList<Object> myList = new LinkedList<Object>();
private int MAX =
private final Lock lock = new ReentrantLock();
private final Condition full = lock
private final Condition empty = lock
public Sycn
}
public void start(){
new Producer()
new Consumer()
}
public static void main(String[] args) throws Exception{
Sycn
s
}
class Producer extends Thread{
public void run(){
while(true){
lock
try{
while(myList
System
full
}
Object o = new Object();
if(myList
System
empty
}
}catch(InterruptedException ie){
System
}finally{
lock
}
}
}
}
class Consumer extends Thread{
public void run(){
while(true){
lock
try{
while(myList
System
empty
}
Object o = myList
System
full
}catch(InterruptedException ie){
System
}finally{
lock
}
}
}
}
}
BlockingQueue也是JDK
它用於阻塞操作的是put()和take()方法
put()方法類似於我們上面的生產者線程
take()方法類似於我們上面的消費者線程
下面是一個例子代碼
import ncurrent
public class Sycn
private LinkedBlockingQueue<Object> queue = new LinkedBlockingQueue<Object>(
private int MAX =
public Sycn
}
public void start(){
new Producer()
new Consumer()
}
public static void main(String[] args) throws Exception{
Sycn
s
}
class Producer extends Thread{
public void run(){
while(true){
//synchronized(this){
try{
if(queue
System
Object o = new Object();
queue
System
}catch(InterruptedException e){
System
}
//}
}
}
}
class Consumer extends Thread{
public void run(){
while(true){
//synchronized(this){
try{
if(queue
System
Object o = queue
System
}catch(InterruptedException e){
System
}
//}
}
}
}
}
你發現這個例子中的問題了嗎?
如果沒有
…
warning: it
Producer: java
…
你可能會說這是因為put()和System
這是因為
對於BlockingQueue大家可以放心使用
對於這種多重嵌套同步的問題
這個類位於java
下面是一個例子代碼
import java
public class Sycn
private PipedOutputStream pos;
private PipedInputStream pis;
//private ObjectOutputStream oos;
//private ObjectInputStream ois;
public Sycn
try{
pos = new PipedOutputStream();
pis = new PipedInputStream(pos);
//oos = new ObjectOutputStream(pos);
//ois = new ObjectInputStream(pis);
}catch(IOException e){
System
}
}
public void start(){
new Producer()
new Consumer()
}
public static void main(String[] args) throws Exception{
Sycn
s
}
class Producer extends Thread{
public void run() {
try{
while(true){
int b = (int) (Math
System
pos
pos
//Object o = new MyObject();
//oos
//oos
//System
}
}catch(Exception e){
//System
e
}finally{
try{
pos
pis
//oos
//ois
}catch(IOException e){
System
}
}
}
}
class Consumer extends Thread{
public void run(){
try{
while(true){
int b = pis
System
//Object o = ois
//if(o != null)
//System
}
}catch(Exception e){
//System
e
}finally{
try{
pos
pis
//oos
//ois
}catch(IOException e){
System
}
}
}
}
//class MyObject implements Serializable {
//}
}
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27617.html