marketmakerclient.java
来自「100多M的J2EE培训内容」· Java 代码 · 共 63 行
JAVA
63 行
package bible.rmi.example4;
import weblogic.rmi.*;
import java.util.*;
import javax.naming.*;
/**
* MarketMakerClient periodically sets prices in the PriceServer.
*/
public class MarketMakerClient extends TimerTask implements Runnable {
/** rand */
private Random rand = new Random();
/** securities */
private String[] securities = null;
/**
* Get a reference to the PriceServer, get a list of securites, and display prices.
*/
public void run() {
System.out.println("MarketMakerClient setting prices.");
System.out.println(" Time: " + new Date(System.currentTimeMillis()));
try {
InitialContext ctx = Environment.getInitialContext();
PriceServer server =
(PriceServer) ctx.lookup(PriceServer.PRICESERVERNAME);
securities = server.getSecurities();
for (int i = 0; i < securities.length; i++) {
boolean increase = rand.nextBoolean();
Float amount = new Float(0.25 * rand.nextInt(2));
if (increase) {
server.setPrice(securities [i],
server.getPrice(securities [i])
+ amount.floatValue());
} else {
server.setPrice(securities [i],
server.getPrice(securities [i])
- amount.floatValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Declare a new client and schedule its functionality on a timer.
* @param args
*/
public static void main(String[] args) {
Timer t = new Timer();
MarketMakerClient client = new MarketMakerClient();
// Starting in 5 secs, re-do pricing every 5 seconds.
t.schedule(client, 5000, 5000);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?