18-java5阻塞队列实现线程间通信-实现线程间通信方式(3)
2015-03-30 05:34
591 查看
一、主要内容
实现子线程运行10次,主线程运行100次,子线程、主线程交替进行,此过程循环进行50次。
二、代码
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class BlockingQueueCommunication {
public static void main(String[] args) {
final Business business = new Business();
// 子线程
new Thread(
new Runnable() {
public void run() {
for(int i=1;i<=50;i++){
business.sub(i);
}
}
}
).start();
// 主线程
for(int i=1;i<=50;i++){
business.main(i);
}
}
static class Business {
// 用于控制子线程
BlockingQueue<Integer> queue1 = new ArrayBlockingQueue<Integer>(1);
// 用于控制主线程
BlockingQueue<Integer> queue2 = new ArrayBlockingQueue<Integer>(1);
{
try {
queue2.put(1); // 控制让子线程先运行
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void sub(int i){
try {
queue1.put(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int j=1;j<=10;j++){
System.out.println("sub thread sequece of " + j + ",loop of " + i);
}
try {
queue2.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void main(int i){
try {
queue2.put(1);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
for(int j=1;j<=100;j++){
System.out.println("main thread sequece of " + j + ",loop of " + i);
}
try {
queue1.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
实现子线程运行10次,主线程运行100次,子线程、主线程交替进行,此过程循环进行50次。
二、代码
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class BlockingQueueCommunication {
public static void main(String[] args) {
final Business business = new Business();
// 子线程
new Thread(
new Runnable() {
public void run() {
for(int i=1;i<=50;i++){
business.sub(i);
}
}
}
).start();
// 主线程
for(int i=1;i<=50;i++){
business.main(i);
}
}
static class Business {
// 用于控制子线程
BlockingQueue<Integer> queue1 = new ArrayBlockingQueue<Integer>(1);
// 用于控制主线程
BlockingQueue<Integer> queue2 = new ArrayBlockingQueue<Integer>(1);
{
try {
queue2.put(1); // 控制让子线程先运行
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void sub(int i){
try {
queue1.put(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int j=1;j<=10;j++){
System.out.println("sub thread sequece of " + j + ",loop of " + i);
}
try {
queue2.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void main(int i){
try {
queue2.put(1);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
for(int j=1;j<=100;j++){
System.out.println("main thread sequece of " + j + ",loop of " + i);
}
try {
queue1.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
相关文章推荐
- 12-使用java5条件阻塞condition实现线程间通信-实现线程间通信方式(2)
- Java阻塞队列线程集控制的实现
- Java可阻塞队列的两种实现方式 (传统wait/notify和jdk1.5以后的lock)
- Java阻塞队列线程集控制的实现
- Java可阻塞队列的两种实现方式
- 黑马程序员——Java5中的线程并发库(二)--Semaphore实现信号灯、其他同步函数、可阻塞的队列、同步集合
- Java可阻塞队列的两种实现方式
- JAVA可阻塞队列-ArrayBlockingQueue子类BlockingQueue的应用,使用它来实现子线程打印10次,主线程打印100次,如此反复
- [疯狂Java]多线程:线程通信(监控器信号、Condition信号、阻塞队列)
- Java -- 使用阻塞队列(BlockingQueue)控制线程通信
- Thinking in Java---线程通信+三种方式实现生产者消费者问题
- 0038 Java学习笔记-多线程-传统线程间通信、Condition、阻塞队列、《疯狂Java讲义 第三版》进程间通信示例代码存在的一个问题
- Java线程间通信-回调的实现方式
- 学习java多线程的笔记3-使用BlockingQueue阻塞队列来模拟两个线程之间的通信
- java分布式开发TCP/IP NIO无阻塞 Socket((基于消息方式实现系统间的通信) )(转)
- Java线程间通信-回调的实现方式
- Lock和Condition实现线程通信(附阻塞队列的实例)
- Java线程间通信-回调的实现方式
- Java线程间通信-回调的实现方式
- Java线程间通信-回调的实现方式