ThreadPoolManager

public void checkIPAuth(String iccid,String ipAddress)
{

AsyncIPAuth as = new AsyncIPAuth(iccid,ipAddress);
boolean isSucceed = false;
ThreadPoolManager poolManager = ThreadPoolManager.getInstance();
int recordCount = 0;
// 如果是线程池中的线程忙,则一直重试
while (!isSucceed)
{
try
{
poolManager.execute(as);
isSucceed = true;
}
catch (RejectedExecutionException ex)
{


isSucceed = false;
recordCount++;
if (recordCount >= PropertiesUtil.getValueInt(
"asyncDoActiveReTryCount", 2))
{
logger.error("pool busy....AsyncIPAuth failed!,as=" + as);
isSucceed = true;
}


try
{
Thread.sleep(1000L);
}
catch (Exception e)
{

logger.error("DeviceServiceImpl.AsyncIPAuth sleep error!",
e);


}


}


}
}


class AsyncIPAuth implements Runnable
{
private String iccid;

private String ipAddress;

/**

* @param iccid
* @param ipAddress
*/
public AsyncIPAuth(String iccid,String ipAddress)
{
this.iccid = iccid;
this.ipAddress = ipAddress;
}


@Override
public void run() {
String realIp = "";
String result = "";
if(!StringUtil.isNil(ipAddress) && ipAddress.length() == 8){
realIp = getIp(ipAddress);
}
StringBuilder sb = new StringBuilder();
String url = sb.append(PropertiesUtil.getValueString("EOC_UP_ONLINEBYIP_AUTH")).toString();

Map<String, String> map = new HashMap<String, String>();
map.put("operate", "1");
map.put("iccid", iccid);
if(!StringUtil.isNil(realIp)){
map.put("ip", realIp);
}

try {
result = RestUtil.getRestContent(url, map);
} catch (IOException e) {
e.printStackTrace();
}

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++中实现线程池的基本步骤如下: 1. 创建一个线程池管理器 ThreadPoolManager,用于管理线程池的状态。 2. 创建一个队列,用于存储任务队列。 3. 创建一组线程,用于执行任务。 4. 将任务添加到任务队列中。 5. 当线程池中的线程被空闲下来时,从任务队列中取出任务并分配给空闲的线程进行执行。 6. 当任务队列为空时,线程池进入等待状态。 7. 当线程池被关闭时,停止所有的线程并清空任务队列。 下面是一个简单的C++线程池实现的代码: ```cpp #include <iostream> #include <queue> #include <thread> #include <mutex> #include <condition_variable> // 任务类 class Task { public: virtual void execute() = 0; }; // 线程池类 class ThreadPoolManager { public: ThreadPoolManager(int threadPoolSize) : poolSize(threadPoolSize), isShutdown(false) { // 创建一组线程,初始化线程池 for (int i = 0; i < poolSize; i++) { std::thread worker(&ThreadPoolManager::run, this); threads.push_back(std::move(worker)); } } ~ThreadPoolManager() { { std::unique_lock<std::mutex> lock(queueMutex); isShutdown = true; } condition.notify_all(); for (auto& thread : threads) { thread.join(); } } void execute(Task* task) { std::unique_lock<std::mutex> lock(queueMutex); taskQueue.push(task); condition.notify_one(); } private: void run() { while (true) { std::unique_lock<std::mutex> lock(queueMutex); condition.wait(lock, [this] { return !taskQueue.empty() || isShutdown; }); if (isShutdown) { break; } Task* task = taskQueue.front(); taskQueue.pop(); lock.unlock(); task->execute(); delete task; } } int poolSize; std::vector<std::thread> threads; std::queue<Task*> taskQueue; std::mutex queueMutex; std::condition_variable condition; bool isShutdown; }; // 自定义任务类 class MyTask : public Task { public: MyTask(int n) : num(n) {} void execute() { std::cout << "Thread " << std::this_thread::get_id() << " executing task " << num << std::endl; } private: int num; }; int main() { ThreadPoolManager pool(4); for (int i = 0; i < 10; i++) { pool.execute(new MyTask(i)); } return 0; } ``` 在上面的代码中,我们创建了一个线程池管理器 ThreadPoolManager 和一个任务类 Task。ThreadPoolManager 类中有一个任务队列 taskQueue 和一组线程 threads,我们可以通过调用 execute() 方法将任务添加到任务队列中。线程池中的每个线程都是一个 std::thread 类的实例,它从任务队列中取出任务并执行。当线程池被关闭时,我们需要调用析构函数( ~ThreadPoolManager()~ )来停止所有的线程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值