我正在向客户发送价格(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,Map
最佳答案
这是一个监听器模式的简单示例,我不确定这种方法是否适合您,但只是抛出一些想法……
原文链接:https://www.f2er.com/spring/432229.htmlimport 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;
Vector