import java.util.concurrent.LinkedBlockingQueue;
public class Test {
public static void main(String[] args) {
LinkedBlockingQueue<String> mQueue = new LinkedBlockingQueue<String>();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
String s = mQueue.take();
System.out.println("取出数据:" + String.valueOf(s));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
int count = 0;
while (true) {
System.out.println("装载数据:" + count);
try {
mQueue.put(String.valueOf(count));
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
}
}
}).start();
}
}
Log:
装载数据:0
取出数据:0
装载数据:1
取出数据:1
装载数据:2
取出数据:2
装载数据:3
取出数据:3
装载数据:4
取出数据:4
装载数据:5
取出数据:5