Scheduler 0.0000001
This commit is contained in:
parent
8b8c052c97
commit
f4bdace79d
@ -25,12 +25,8 @@
|
||||
*/
|
||||
package gui;
|
||||
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.UnsupportedLookAndFeelException;
|
||||
import sesim.AutoTrader;
|
||||
import sesim.Exchange;
|
||||
import traders.RandomTrader;
|
||||
import traders.RandomTraderConfig;
|
||||
import traders.*;
|
||||
|
||||
|
||||
@ -205,7 +201,8 @@ public class MainWin extends javax.swing.JFrame {
|
||||
//rt.start();
|
||||
|
||||
// SwitchingTraderConfig rcfg1 = new SwitchingTraderConfig();
|
||||
SwitchingTraderConfig rcfg1 = new SwitchingTraderConfig();
|
||||
// SwitchingTraderConfig rcfg1 = new SwitchingTraderConfig();
|
||||
RandomTraderConfig rcfg1 = new RandomTraderConfig();
|
||||
AutoTrader rt1 = rcfg1.createTrader(se, 0, 1000000);
|
||||
|
||||
rt1.setName("Bob");
|
||||
@ -214,9 +211,10 @@ public class MainWin extends javax.swing.JFrame {
|
||||
//rt2.start();
|
||||
|
||||
|
||||
SwitchingTraderConfig cfg = new SwitchingTraderConfig();
|
||||
// SwitchingTraderConfig cfg = new SwitchingTraderConfig();
|
||||
RandomTraderConfig cfg= new RandomTraderConfig();
|
||||
|
||||
for (int i=0; i<1000; i++){
|
||||
for (int i=0; i<10; i++){
|
||||
AutoTrader randt = cfg.createTrader(se, 100000, 0);
|
||||
|
||||
randt.setName("Alice");
|
||||
@ -263,10 +261,8 @@ public class MainWin extends javax.swing.JFrame {
|
||||
//</editor-fold>
|
||||
|
||||
/* Create and display the form */
|
||||
java.awt.EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
new MainWin().setVisible(true);
|
||||
}
|
||||
java.awt.EventQueue.invokeLater(() -> {
|
||||
new MainWin().setVisible(true);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -2,12 +2,6 @@
|
||||
|
||||
<Form version="1.9" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
|
||||
<NonVisualComponents>
|
||||
<Container class="javax.swing.JDesktopPane" name="desktopPane">
|
||||
|
||||
<Layout class="org.netbeans.modules.form.compat2.layouts.DesignAbsoluteLayout">
|
||||
<Property name="useNullLayout" type="boolean" value="true"/>
|
||||
</Layout>
|
||||
</Container>
|
||||
<Menu class="javax.swing.JMenuBar" name="menuBar">
|
||||
<SubComponents>
|
||||
<Menu class="javax.swing.JMenu" name="fileMenu">
|
||||
@ -124,13 +118,39 @@
|
||||
<Layout>
|
||||
<DimensionLayout dim="0">
|
||||
<Group type="103" groupAlignment="0" attributes="0">
|
||||
<EmptySpace min="0" pref="400" max="32767" attributes="0"/>
|
||||
<Group type="102" alignment="1" attributes="0">
|
||||
<Component id="orderBookPanel1" pref="226" max="32767" attributes="0"/>
|
||||
<EmptySpace type="unrelated" max="-2" attributes="0"/>
|
||||
<Component id="bidBook1" min="-2" max="-2" attributes="0"/>
|
||||
<EmptySpace max="-2" attributes="0"/>
|
||||
</Group>
|
||||
<Component id="desktopPane" alignment="0" max="32767" attributes="0"/>
|
||||
</Group>
|
||||
</DimensionLayout>
|
||||
<DimensionLayout dim="1">
|
||||
<Group type="103" groupAlignment="0" attributes="0">
|
||||
<EmptySpace min="0" pref="279" max="32767" attributes="0"/>
|
||||
<Group type="102" attributes="0">
|
||||
<Component id="desktopPane" min="-2" pref="108" max="-2" attributes="0"/>
|
||||
<EmptySpace pref="48" max="32767" attributes="0"/>
|
||||
<Group type="103" groupAlignment="0" attributes="0">
|
||||
<Component id="orderBookPanel1" min="-2" pref="310" max="-2" attributes="0"/>
|
||||
<Component id="bidBook1" min="-2" pref="218" max="-2" attributes="0"/>
|
||||
</Group>
|
||||
<EmptySpace max="-2" attributes="0"/>
|
||||
</Group>
|
||||
</Group>
|
||||
</DimensionLayout>
|
||||
</Layout>
|
||||
<SubComponents>
|
||||
<Component class="gui.BidBook" name="bidBook1">
|
||||
</Component>
|
||||
<Container class="javax.swing.JDesktopPane" name="desktopPane">
|
||||
|
||||
<Layout class="org.netbeans.modules.form.compat2.layouts.DesignAbsoluteLayout">
|
||||
<Property name="useNullLayout" type="boolean" value="true"/>
|
||||
</Layout>
|
||||
</Container>
|
||||
<Component class="gui.OrderBookPanel" name="orderBookPanel1">
|
||||
</Component>
|
||||
</SubComponents>
|
||||
</Form>
|
||||
|
@ -47,7 +47,9 @@ public class NewMDIApplication extends javax.swing.JFrame {
|
||||
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
|
||||
private void initComponents() {
|
||||
|
||||
bidBook1 = new gui.BidBook();
|
||||
desktopPane = new javax.swing.JDesktopPane();
|
||||
orderBookPanel1 = new gui.OrderBookPanel();
|
||||
menuBar = new javax.swing.JMenuBar();
|
||||
fileMenu = new javax.swing.JMenu();
|
||||
openMenuItem = new javax.swing.JMenuItem();
|
||||
@ -132,11 +134,22 @@ public class NewMDIApplication extends javax.swing.JFrame {
|
||||
getContentPane().setLayout(layout);
|
||||
layout.setHorizontalGroup(
|
||||
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.addGap(0, 400, Short.MAX_VALUE)
|
||||
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
|
||||
.addComponent(orderBookPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
|
||||
.addComponent(bidBook1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
.addContainerGap())
|
||||
.addComponent(desktopPane)
|
||||
);
|
||||
layout.setVerticalGroup(
|
||||
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.addGap(0, 279, Short.MAX_VALUE)
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addComponent(desktopPane, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
|
||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.addComponent(orderBookPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
.addComponent(bidBook1, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE))
|
||||
.addContainerGap())
|
||||
);
|
||||
|
||||
pack();
|
||||
@ -183,6 +196,7 @@ public class NewMDIApplication extends javax.swing.JFrame {
|
||||
|
||||
// Variables declaration - do not modify//GEN-BEGIN:variables
|
||||
private javax.swing.JMenuItem aboutMenuItem;
|
||||
private gui.BidBook bidBook1;
|
||||
private javax.swing.JMenuItem contentMenuItem;
|
||||
private javax.swing.JMenuItem copyMenuItem;
|
||||
private javax.swing.JMenuItem cutMenuItem;
|
||||
@ -194,6 +208,7 @@ public class NewMDIApplication extends javax.swing.JFrame {
|
||||
private javax.swing.JMenu helpMenu;
|
||||
private javax.swing.JMenuBar menuBar;
|
||||
private javax.swing.JMenuItem openMenuItem;
|
||||
private gui.OrderBookPanel orderBookPanel1;
|
||||
private javax.swing.JMenuItem pasteMenuItem;
|
||||
private javax.swing.JMenuItem saveAsMenuItem;
|
||||
private javax.swing.JMenuItem saveMenuItem;
|
||||
|
@ -1,101 +0,0 @@
|
||||
package sesim;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
final public class Account_old {
|
||||
|
||||
/**
|
||||
* Exchange this account belongs to
|
||||
*/
|
||||
public Exchange se;
|
||||
|
||||
/**
|
||||
* Number of shares in this account
|
||||
*/
|
||||
public long shares = 0;
|
||||
|
||||
/**
|
||||
* Ammount of money in this account
|
||||
*/
|
||||
public double money = 0;
|
||||
|
||||
/**
|
||||
* Name of this account
|
||||
*/
|
||||
public String name = "";
|
||||
|
||||
|
||||
public ArrayList <Order_old> pending;
|
||||
|
||||
public boolean orderpending = false;
|
||||
|
||||
|
||||
public Account_old(Exchange se, long shares, double money ) {
|
||||
this.shares=shares;
|
||||
this.money=money;
|
||||
this.se=se;
|
||||
pending = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Account_old(){
|
||||
//this(,0.0);
|
||||
}
|
||||
|
||||
// private double bound_money;
|
||||
|
||||
|
||||
|
||||
public void print_current() {
|
||||
System.out.printf("%s shares: %d credit: %.2f\n",
|
||||
name, shares, money
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public boolean isRuined(){
|
||||
|
||||
|
||||
/* System.out.print(
|
||||
"Account_old: "
|
||||
+money
|
||||
+" / "
|
||||
+shares
|
||||
+"\n"
|
||||
);
|
||||
*/
|
||||
return this.money<=se.lastprice && this.shares<=0;
|
||||
}
|
||||
|
||||
/*
|
||||
public Order_old sell(long volume, double limit) {
|
||||
SellOrder o = new SellOrder();
|
||||
o.account = this;
|
||||
o.limit = limit;
|
||||
o.volume = volume;
|
||||
orderpending = true;
|
||||
return se.SendOrder(o);
|
||||
}
|
||||
*/
|
||||
/* public Order_old buy(long volume, double limit) {
|
||||
if (volume * limit > money) {
|
||||
return null;
|
||||
}
|
||||
BuyOrder o = new BuyOrder();
|
||||
o.limit = limit;
|
||||
o.volume = volume;
|
||||
o.account = this;
|
||||
orderpending = true;
|
||||
return se.SendOrder(o);
|
||||
}
|
||||
/*
|
||||
/*
|
||||
public void Buy(Account_old a, long size, double price) {
|
||||
shares += size;
|
||||
money -= price * size;
|
||||
a.shares -= size;
|
||||
a.money += price * size;
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
@ -25,11 +25,26 @@
|
||||
*/
|
||||
package sesim;
|
||||
|
||||
import java.util.SortedSet;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class AutoTraderLIst {
|
||||
public class AutoTraderList {
|
||||
|
||||
SortedSet <AutoTrader> traders;
|
||||
|
||||
|
||||
AutoTraderList(){
|
||||
|
||||
}
|
||||
|
||||
List getTraders(){
|
||||
|
||||
|
||||
return new ArrayList();
|
||||
}
|
||||
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package sesim;
|
||||
|
||||
public class BuyOrder extends Order_old implements Comparable<Order_old> {
|
||||
|
||||
public BuyOrder(){
|
||||
super(OrderType_old.bid);
|
||||
}
|
||||
|
||||
}
|
@ -2,10 +2,8 @@ package sesim;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import sesim.Order_old.OrderType_old;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -168,9 +166,7 @@ public class Exchange { //extends Thread {
|
||||
* Constructor
|
||||
*/
|
||||
public Exchange() {
|
||||
this.ask = new TreeSet<>();
|
||||
this.bid = new TreeSet<>();
|
||||
// this.qrlist = new ArrayList<>();
|
||||
|
||||
this.qrlist = (new CopyOnWriteArrayList<>());
|
||||
|
||||
// Create order books
|
||||
@ -316,23 +312,12 @@ public class Exchange { //extends Thread {
|
||||
double lastprice = 100.0;
|
||||
long lastsvolume;
|
||||
|
||||
public TreeSet<Order_old> bid;
|
||||
public TreeSet<Order_old> ask;
|
||||
|
||||
|
||||
private final Locker tradelock = new Locker();
|
||||
|
||||
|
||||
private TreeSet<Order_old> selectOrderBook(OrderType_old t) {
|
||||
|
||||
switch (t) {
|
||||
case bid:
|
||||
return this.bid;
|
||||
case ask:
|
||||
return this.ask;
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public ArrayList<Order> getOrderBook(OrderType type, int depth) {
|
||||
|
||||
@ -356,43 +341,9 @@ public class Exchange { //extends Thread {
|
||||
return this.quoteHistory.first();
|
||||
}
|
||||
|
||||
public void print_current() {
|
||||
|
||||
Order_old b;
|
||||
Order_old a;
|
||||
|
||||
//String BID;
|
||||
if (bid.isEmpty()) {
|
||||
b = new BuyOrder();
|
||||
b.limit = -1;
|
||||
b.volume = 0;
|
||||
} else {
|
||||
b = bid.first();
|
||||
}
|
||||
|
||||
if (ask.isEmpty()) {
|
||||
a = new SellOrder();
|
||||
a.limit = -1;
|
||||
a.volume = 0;
|
||||
|
||||
} else {
|
||||
a = ask.first();
|
||||
}
|
||||
|
||||
Logger.info(String.format("BID: %s(%s) LAST: %.2f(%d) ASK: %s(%s)\n",
|
||||
b.format_limit(), b.format_volume(),
|
||||
lastprice, lastsvolume,
|
||||
a.format_limit(), a.format_volume())
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
public void transferMoney(Account_old src, Account_old dst, double money) {
|
||||
src.money -= money;
|
||||
dst.money += money;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void transferMoneyAndShares(Account src, Account dst, double money, double shares) {
|
||||
src.money -= money;
|
||||
dst.money += money;
|
||||
@ -431,33 +382,9 @@ public class Exchange { //extends Thread {
|
||||
*
|
||||
* @param o
|
||||
*/
|
||||
public void cancelOrder_old(Order_old o) {
|
||||
tradelock.lock();
|
||||
TreeSet<Order_old> book = this.selectOrderBook(o.type);
|
||||
book.remove(o);
|
||||
/* this.updateBookReceivers(o.type);
|
||||
o.account.pending.remove(o);
|
||||
o.status = OrderStatus.canceled;
|
||||
tradelock.unlock();
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfer shares from one account to another account
|
||||
*
|
||||
* @param src source account
|
||||
* @param dst destination account
|
||||
* @param volumen number of shares
|
||||
* @param price price
|
||||
*/
|
||||
protected void transferShares(Account_old src, Account_old dst, long volume, double price) {
|
||||
dst.shares += volume;
|
||||
src.shares -= volume;
|
||||
dst.money -= price * volume;
|
||||
src.money += price * volume;
|
||||
}
|
||||
|
||||
|
||||
|
||||
long nextQuoteId = 0;
|
||||
|
||||
private void removeOrderIfExecuted(Order o) {
|
||||
|
@ -27,7 +27,9 @@ package sesim;
|
||||
|
||||
import java.util.concurrent.Semaphore;
|
||||
|
||||
|
||||
/**
|
||||
* A locker object
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
@ -37,6 +39,7 @@ public class Locker {
|
||||
|
||||
/**
|
||||
* Acquire a lock
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean lock() {
|
||||
@ -48,6 +51,9 @@ public class Locker {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Release a lock
|
||||
*/
|
||||
public void unlock() {
|
||||
AVAIL.release();
|
||||
}
|
||||
|
@ -1,105 +0,0 @@
|
||||
package sesim;
|
||||
|
||||
public abstract class Order_old implements Comparable<Order_old> {
|
||||
|
||||
/**
|
||||
* When the order was created
|
||||
*/
|
||||
public long timestamp = 0;
|
||||
|
||||
/**
|
||||
* Number of shares
|
||||
*/
|
||||
public long volume;
|
||||
|
||||
/**
|
||||
* Limit price
|
||||
*/
|
||||
public double limit;
|
||||
|
||||
/**
|
||||
* Order_old ID
|
||||
*/
|
||||
public long id = 0;
|
||||
|
||||
/**
|
||||
* Type of order
|
||||
*/
|
||||
public final OrderType_old type;
|
||||
|
||||
public Account_old account = null;
|
||||
|
||||
|
||||
protected int compareLimit(Order_old o){
|
||||
|
||||
int r=0;
|
||||
if (o.limit < limit) {
|
||||
r=-1;
|
||||
}
|
||||
if (o.limit > limit) {
|
||||
r=1;
|
||||
}
|
||||
|
||||
if (type==OrderType_old.ask)
|
||||
return -r;
|
||||
|
||||
return r;
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public int compareTo(Order_old o) {
|
||||
|
||||
if (o.type!=type){
|
||||
System.out.print("OrderType Missmatch\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int r = compareLimit(o);
|
||||
if (r!=0)
|
||||
return r;
|
||||
|
||||
|
||||
if (o.id>id)
|
||||
return -1;
|
||||
|
||||
if (o.id<id)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
enum OrderStatus {
|
||||
open, executed, canceled
|
||||
}
|
||||
|
||||
public enum OrderType_old {
|
||||
bid,ask
|
||||
}
|
||||
|
||||
OrderStatus status = OrderStatus.open;
|
||||
|
||||
public long getAge() {
|
||||
if (timestamp == 0) {
|
||||
return 0;
|
||||
}
|
||||
return System.currentTimeMillis() - timestamp;
|
||||
}
|
||||
|
||||
String format_limit() {
|
||||
if (limit < 0.0) {
|
||||
return "n.a.";
|
||||
}
|
||||
return String.format("%.2f", limit);
|
||||
}
|
||||
|
||||
String format_volume() {
|
||||
return String.format("%d", volume);
|
||||
}
|
||||
|
||||
Order_old(OrderType_old type) {
|
||||
this.type=type;
|
||||
}
|
||||
}
|
231
src/main/java/sesim/Scheduler.java
Normal file
231
src/main/java/sesim/Scheduler.java
Normal file
@ -0,0 +1,231 @@
|
||||
/*
|
||||
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package sesim;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.SortedMap;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.ConcurrentSkipListSet;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class Scheduler extends Thread {
|
||||
|
||||
long multiply = 1;
|
||||
final SortedMap<Long, SortedSet<TimerEvent>> event_queue = new TreeMap<>();
|
||||
|
||||
private boolean stop = false;
|
||||
|
||||
public class Runner extends Thread {
|
||||
|
||||
Runner() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (!stop) {
|
||||
synchronized (event_queue) {
|
||||
try {
|
||||
event_queue.wait(5000);
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
System.out.printf("500 mllies waited\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class Event implements Comparable {
|
||||
|
||||
long time;
|
||||
|
||||
@Override
|
||||
public int compareTo(Object o) {
|
||||
if (((Event) o).time < time) {
|
||||
return -1;
|
||||
}
|
||||
if (((Event) o).time > time) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Runner queues[];
|
||||
Runner runners[];
|
||||
|
||||
/**
|
||||
*
|
||||
* @param num_runners
|
||||
*/
|
||||
public Scheduler(int num_runners) {
|
||||
// event_queue = new ConcurrentSkipListSet(new TreeSet<>());
|
||||
runners = new Runner[num_runners];
|
||||
|
||||
for (int i = 0; i < num_runners; i++) {
|
||||
Runner r = new Runner();
|
||||
System.out.print("Making runner: " + r + "\n");
|
||||
|
||||
runners[i] = new Runner();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void start() {
|
||||
|
||||
/* for (Runner runner : runners) {
|
||||
runner.start();
|
||||
}
|
||||
*/
|
||||
super.start();
|
||||
}
|
||||
|
||||
/*
|
||||
public boolean isAlive() {
|
||||
|
||||
for (Runner runner : runners) {
|
||||
if (runner.isAlive()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
public void halt() {
|
||||
stop = true;
|
||||
synchronized (event_queue) {
|
||||
event_queue.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
public interface TimerEvent {
|
||||
|
||||
long timerEvent();
|
||||
|
||||
}
|
||||
|
||||
class ObjectComparator implements Comparator<Object> {
|
||||
|
||||
@Override
|
||||
public int compare(Object o1, Object o2) {
|
||||
return System.identityHashCode(o1) - System.identityHashCode(o1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public long getCurrentTimeMillies() {
|
||||
return System.currentTimeMillis();
|
||||
|
||||
}
|
||||
|
||||
public void startEvent(TimerEvent e, long time) {
|
||||
long evtime = time + this.getCurrentTimeMillies();
|
||||
synchronized (event_queue) {
|
||||
SortedSet<TimerEvent> s = event_queue.get(evtime);
|
||||
if (s == null) {
|
||||
s = new TreeSet<>(new ObjectComparator());
|
||||
event_queue.put(evtime, s);
|
||||
}
|
||||
s.add(e);
|
||||
}
|
||||
synchronized(this){
|
||||
notify();
|
||||
}
|
||||
}
|
||||
|
||||
public long fireEvent(TimerEvent e) {
|
||||
return e.timerEvent();
|
||||
}
|
||||
|
||||
private void addEvent(TimerEvent e, long time) {
|
||||
long evtime = time + this.getCurrentTimeMillies();
|
||||
SortedSet<TimerEvent> s = event_queue.get(evtime);
|
||||
if (s == null) {
|
||||
s = new TreeSet<>(new ObjectComparator());
|
||||
event_queue.put(evtime, s);
|
||||
}
|
||||
s.add(e);
|
||||
}
|
||||
|
||||
public long runEvents() {
|
||||
synchronized (event_queue) {
|
||||
if (event_queue.isEmpty()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
long t = event_queue.firstKey();
|
||||
if (t >= this.getCurrentTimeMillies()) {
|
||||
SortedSet s = event_queue.get(t);
|
||||
event_queue.remove(t);
|
||||
Iterator<TimerEvent> it = s.iterator();
|
||||
while (it.hasNext()) {
|
||||
long next_t = this.fireEvent(it.next());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (!stop) {
|
||||
long wtime = runEvents();
|
||||
|
||||
synchronized (this) {
|
||||
try {
|
||||
if (wtime != -1) {
|
||||
wait(wtime);
|
||||
} else {
|
||||
wait();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.print("Running the scheduler\n");
|
||||
}
|
||||
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
package sesim;
|
||||
|
||||
public class SellOrder extends Order_old {
|
||||
|
||||
public SellOrder(){
|
||||
super(OrderType_old.ask);
|
||||
}
|
||||
}
|
@ -25,6 +25,9 @@
|
||||
*/
|
||||
package sesim;
|
||||
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
|
||||
|
||||
|
||||
@ -63,46 +66,48 @@ public class Test {
|
||||
* @param args the command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Exchange se = new Exchange();
|
||||
|
||||
Scheduler s = new Scheduler(10);
|
||||
s.start();
|
||||
|
||||
class Ev implements Scheduler.TimerEvent{
|
||||
|
||||
double aid1 = se.createAccount(100, 100);
|
||||
double aid2 = se.createAccount(100, 100);
|
||||
@Override
|
||||
public long timerEvent() {
|
||||
System.out.print("Timer Event Occured");
|
||||
return 3000;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Ev e1 = new Ev();
|
||||
Ev e2 = new Ev();
|
||||
|
||||
|
||||
sesim.Exchange.Account a = se.getAccount(aid1);
|
||||
System.out.print(a.getMoney());
|
||||
s.startEvent(e1, 0);
|
||||
|
||||
tube();
|
||||
while(true){}
|
||||
// s.startEvent(e2, 100);
|
||||
|
||||
|
||||
|
||||
|
||||
/* long starttime=System.currentTimeMillis();
|
||||
while (s.isAlive()){
|
||||
if (System.currentTimeMillis()>starttime+6650){
|
||||
s.stop();
|
||||
break;
|
||||
}
|
||||
}
|
||||
System.out.print("Waiting fpor Stop\n");
|
||||
while (s.isAlive()){
|
||||
|
||||
}
|
||||
|
||||
System.out.print("All isstopped\n");
|
||||
*/
|
||||
|
||||
|
||||
System.exit(0);
|
||||
|
||||
/*
|
||||
|
||||
AccountData a1 = se.getAccountData(aid1);
|
||||
AccountData a2 = se.getAccountData(aid2);
|
||||
Test.print_account(a1);
|
||||
Test.print_account(a2);
|
||||
|
||||
se.createOrder(aid2, Exchange.OrderType.ASK, 20, 11);
|
||||
se.createOrder(aid2, Exchange.OrderType.ASK, 10, 10);
|
||||
se.createOrder(aid2, Exchange.OrderType.ASK, 10, 9);
|
||||
se.createOrder(aid1, Exchange.OrderType.BID, 50, 11);
|
||||
|
||||
System.out.print("Exec Orders\n");
|
||||
se.executeOrders();
|
||||
System.out.print("Executed Orders\n");
|
||||
|
||||
a1 = se.getAccountData(aid1);
|
||||
a2 = se.getAccountData(aid2);
|
||||
Test.print_account(a1);
|
||||
Test.print_account(a2);
|
||||
|
||||
|
||||
|
||||
|
||||
//S/ystem.out.print(aid);
|
||||
//System.out.print("\n");
|
||||
*/
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user