package com.atm.cloud;
import com.netflix.config.ConfigurationManager;
public class HelloMain {
public static void main(String[] args) throws Exception {
// 設定線程池(超過3,認為是線程池滿載)
ConfigurationManager.getConfigInstance().setProperty(
"hystrix.threadpool.default.coreSize", );
for (int i = ; i < ; i++) {
MyCommand c = new MyCommand(i);
c.queue();//異步執行
}
Thread.sleep();
}
}
信号量隔離政策
package com.atm.cloud;
import com.netflix.config.ConfigurationManager;
import com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStrategy;
public class SemaphoreMain {
public static void main(String[] args) throws Exception {
// 信号量政策,預設最大并發數10
ConfigurationManager.getConfigInstance().setProperty(
"hystrix.command.default.execution.isolation.strategy",
ExecutionIsolationStrategy.SEMAPHORE);
// 設定最大并發數為2
ConfigurationManager
.getConfigInstance()
.setProperty(
"hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests",
);
for (int i = ; i < ; i++) {
final int index = i;
Thread t = new Thread() {
public void run() {
MyCommand c = new MyCommand(index);
c.execute();
}
};
t.start();
}
Thread.sleep();
}
}