我正在向客户发送价格(10000)但是代码下面有循环导致客户等待计算的过程延迟.
PriceVisibleForCustomer =价格CustomerMargin
价格 – 每300毫秒更换一次 – 从中央商店发送,与客户实例无关
CustomerMargn – 由客户协议/分部/管理员决定等产生的一些加或减金额.在客户http会话期间没有变化,我可以将其保留在内存中
客户 – 他登录后参与了这个过程,他应该看到8种产品价格的快速变化.
也许我需要更多的技术?我有Spring 3/4,Java,Weblogic,我可以为此任务创建甚至单独的webapp,以提供计算价格.
我考虑过Java中的线程,但10000个客户意味着太多的线程不是吗?如何更改此代码?也许我应该改变架构,但是如何?
/**
* Sends prices to customer. This method is called very often (300ms) as prices are changing in real time.
* Customer should see prices also each 300ms
* @param productId - id of a product that prices will be calculated
* @param productIdToPriceMap
* @param customerIdToMarginMap - this map is changed every time customer logs in or logs out
*/
private static void sendPricesToCustomers(Long productId,Mapculated. I Could create threads,10000+ customers will be logged in,I cant create so much threads... can I?
for (Long customerId: customerIdToMarginMap.keySet()){
BigDecimal customerMargin = customerIdToMarginMap.get(customerId);
BigDecimal priceResult = productIdToPriceMap.get(productId).add(customerMargin);
//send priceResult to websocket
}
}
最佳答案
这是一个监听器模式的简单示例,我不确定这种方法是否适合您,但只是抛出一些想法……
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.Timer;
public class Demo {
public static Product[] PRODUCTS = new Product[]{
new Product("Computer",400),new Product("Desk",800),new Product("Chair",70),new Product("Printer",300),new Product("Television",200)
};
public static void main(String[] args) throws InterruptedException {
Customer john = new Customer("John",3);
john.addProduct(PRODUCTS[1]);
john.addProduct(PRODUCTS[2]);
john.addProduct(PRODUCTS[3]);
Customer mary = new Customer("Mary",2);
mary.addProduct(PRODUCTS[1]);
mary.addProduct(PRODUCTS[2]);
mary.addProduct(PRODUCTS[4]);
Thread.sleep(10000);
System.exit(0);
}
}
interface IPriceListener {
public void priceChanged(Product product,int price);
}
class Customer implements IPriceListener {
String _name;
int _margin;
Vectorstem.out.println("[" + _name + "][" + _products.get(_products.indexOf(product)).getName() + "][" + price + "][" + (price + _margin) + "]");
}
}
class Product implements ActionListener {
private int _startingPrice;
private int _currentPrice;
private String _name;
private Timer _timer;
private Vector
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。