1.运行方法
public void test() throws Exception {
//定义阻塞队列长度
ArrayBlockingQueue<String> blockingDeque = new ArrayBlockingQueue<String>(20);
//定义生产者
Producer producer = new Producer(blockingDeque);
//定义消费者
Consumer consumer = new Consumer(blockingDeque);
//定义阻塞队列,5为线程数为5
ExecutorService service = Executors.newScheduledThreadPool(5);
//生产者生产
service.execute(producer);
//消费者消费生产者
//注意,此处写了10个消费者,但是由于线程池数量为5,所以消费者开启的线程实际上是4个
for (int i = 0; i <10 ; i++) {
System.out.println("消费者"+i);
service.execute(consumer);
}
}
2.生产者
import lombok.SneakyThrows;
import java.util.concurrent.ArrayBlockingQueue;
/**
* @Description:
* @Author:ay
* @Date:2020/10/14
*/
public class Producer implements Runnable {
private ArrayBlockingQueue<String> arrayBlockingQueue;
public Producer(ArrayBlockingQueue<String> arrayBlockingQueue) {
this.arrayBlockingQueue = arrayBlockingQueue;
}
@Override
public void run() {
System.out.println("生产者"+Thread.currentThread().getName());
int i = 100;
System.out.println("我是生产者"+i);
while (i<999){
try {
arrayBlockingQueue.put("生产者"+i++);
System.out.println("放进去的元素是: 生产者"+i);
} catch (InterruptedException e) {
System.out.println("生产者停止生产");
e.printStackTrace();
}
}
}
}
3.消费者
import com.alibaba.fastjson.JSONObject;
import com.geostar.zrzy.zwfw.dao.BdcYbdyhDao;
import com.geostar.zrzy.zwfw.entity.BdcYbdyh;
import com.geostar.zrzy.zwfw.service.BdcYbdyhService;
import com.geostar.zrzy.zwfw.utils.HttpClientUtils;
import lombok.Data;
import org.apache.avalon.framework.context.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jca.context.SpringContextResourceAdapter;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
/**
* @Description:
* @Author:ay
* @Date:2020/10/14
*/
@Data
public class Consumer implements Runnable {
BdcYbdyhDao bdcYbdyhDao = AppBean.getBean(BdcYbdyhDao.class);
BdcYbdyhService bdcYbdyhService =AppBean.getBean(BdcYbdyhService.class);
private ArrayBlockingQueue<String> arrayBlockingQueue;
public Consumer(ArrayBlockingQueue<String> arrayBlockingQueue) {
this.arrayBlockingQueue = arrayBlockingQueue;
}
@Override
public void run() {
System.out.println("消费者"+Thread.currentThread().getName());
while (true){
try {
String take = arrayBlockingQueue.take();
System.out.println(take);
/
System.out.println("我现在进入了方法");
String dbcode = "18";
List<Map<String, String>> list = new ArrayList<>();
List<Map<String, String>> danrenlist = new ArrayList<>();
List<Map<String, String>> duoRenlist = new ArrayList<>();
Map<String,List> hashMap = new HashMap<>();
System.out.println("hahha"+take);
Map<String, String> map = new HashMap<>();
Map<String, Object> reqMap = new HashMap<>();
reqMap.put("DBCode", dbcode);
reqMap.put("iCode", take);
String result = HttpClientUtils.executePostString("url****", reqMap, "utf-8", 30 * 1000);
JSONObject jsonObject = JSONObject.parseObject(result);
String fwyt = bdcYbdyhDao.getFwyt(take);
List<String> fwytlist = new ArrayList();
fwytlist.add("住宅");
fwytlist.add("商业服务");
fwytlist.add("成套别墅");
fwytlist.add("别墅");
fwytlist.add("高档公寓");
fwytlist.add("非成套住宅");
fwytlist.add("集体宿舍");
fwytlist.add("商业、金融、信息");
fwytlist.add("商业服务");
fwytlist.add("经营");
fwytlist.add("旅游");
fwytlist.add("金融保险");
fwytlist.add("电讯信息");
if (jsonObject != null) {
if ("200".equals(jsonObject.getString("code"))) {
JSONObject data = JSONObject.parseObject(result).getJSONObject("data");
if (data != null) {
int len = 0;
if (data.get("APPLYMAN2").toString().contains(",")) {
len = data.get("APPLYMAN2").toString().split(",").length;
} else if (data.get("APPLYMAN2").toString().contains(" ")) {
len = data.get("APPLYMAN2").toString().split(" ").length;
}
// System.out.println(len);
if(len ==0){
if (fwytlist.contains(fwyt)){
map.put("fwyt", fwyt);
map.put("bdcdyh", bdcYbdyhService.getBdcdyh(take));
danrenlist.add(map);
hashMap.put("房子",danrenlist);
}
}
}
}
}
if(!hashMap.isEmpty()){
File file = new File("D:/testthead.txt");
OutputStream outputStream = new FileOutputStream(file,true);
outputStream.write(JSONObject.toJSONString(hashMap).getBytes("UTF-8"));
outputStream.close();
}
} catch (InterruptedException e) {
System.out.println("消费者在等待");
e.printStackTrace();
} catch (Exception e) {
System.out.println("解析错误!");
e.printStackTrace();
}
}
}
}
代码不全,有些隐私事项不方便展示,大家参考原理使用