ConcurrentSkipListMap firstKey() throws NoSuchElementException even though it contains data - java

I wrote a small application that receives data from a web socket, which I store in static ConcurrentSkipListMap.
The application initially creates a new thread where it runs infinitely while loop calling ConcurrentSkipListMap.firstKey(). After a while, this call throws a NoSuchElementException, even though the ConcurrentSkipListMap contains data.
break point in catch block
Example of my application:
I have cacher class that contains websocket implementation and NavigableMap init:
package solvethat.net.triobot.Example;
import com.binance.api.client.BinanceApiCallback;
import com.binance.api.client.BinanceApiClientFactory;
import com.binance.api.client.domain.event.DepthEvent;
import com.binance.api.client.domain.market.OrderBook;
import com.binance.api.client.domain.market.OrderBookEntry;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.NavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
public class AskCacher {
private long updateId;
private final BinanceApiClientFactory factory;
public AskCacher() {
factory = BinanceApiClientFactory.newInstance();
initAsks();
runWebsocket();
}
/**
* Init data getting order book snapshot
*/
private void initAsks() {
try {
OrderBook orderBook = factory.newRestClient().getOrderBook("btcusdt".toUpperCase(), 10);
updateId = orderBook.getLastUpdateId();
NavigableMap<Double, Double> asks = new ConcurrentSkipListMap<>(Comparator.naturalOrder());
for (OrderBookEntry ask : orderBook.getAsks()) {
asks.put(Double.parseDouble(ask.getPrice()), Double.parseDouble(ask.getQty()));
}
StaticData.ask = asks;
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
private void runWebsocket() {
factory.newWebSocketClient().onDepthEvent("btcusdt", new BinanceApiCallback<>() {
/**
* Set ask price and call analysis method
*/
#Override
public void onResponse(DepthEvent depthEvent) {
if (depthEvent.getFinalUpdateId() > updateId) {
updateId = depthEvent.getFinalUpdateId();
updateOrderBook(depthEvent.getAsks());
}
}
/**
* Just print err message
*/
#Override
public void onFailure(final Throwable cause) {
System.err.println(cause.getMessage());
}
});
}
/**
* Updates an order book (asks) with a delta received from the server.
* Whenever the qty specified is ZERO, it means the price should was removed from the order book.
*/
private void updateOrderBook(List<OrderBookEntry> orderBookDeltas) {
for (OrderBookEntry orderBookDelta : orderBookDeltas) {
Double price = Double.parseDouble(orderBookDelta.getPrice());
BigDecimal qty = new BigDecimal(orderBookDelta.getQty());
if (qty.compareTo(BigDecimal.ZERO) == 0) {
// qty=0 means remove this level
StaticData.ask.remove(price);
} else {
StaticData.ask.put(price, Double.parseDouble(orderBookDelta.getQty()));
}
}
// Print best ask to see if cacher is alive
System.out.println("btc-usdt best ask: " + StaticData.ask.firstKey());
// Edit map length
if (StaticData.ask.size() > 10) {
StaticData.ask.tailMap((Double) StaticData.ask.keySet().toArray()[10], true).clear();
}
}}
Then infinite loop:
package solvethat.net.triobot.Example;
public class InfiniteLoop {
public void loopProcess() {
Analyzer analyzer = new Analyzer();
while (true) {
analyzer.analyze(StaticData.ask.firstEntry());
}
}}
And analyzer class:
package solvethat.net.triobot.Example;
import java.util.Map;
public class Analyzer {
public void analyze(Map.Entry<Double, Double> entry) {
StaticData.AnalyzeObject analyzeObject = new StaticData.AnalyzeObject();
analyzeObject.setBestAsk(entry.getKey());
if (analyzeObject.getBestAsk() > 50000) {
System.out.println("It is a good price!!");
}
}
}
Static data model:
package solvethat.net.triobot.Example;
import java.util.NavigableMap;
public class StaticData {
public static NavigableMap<Double, Double> ask;
public static class AnalyzeObject {
double bestAsk;
public double getBestAsk() {
return bestAsk;
}
public void setBestAsk(double bestAsk) {
this.bestAsk = bestAsk;
}
}
}
Main class for example run:
package solvethat.net.triobot.Example;
public class Main {
public static void main(String[] arguments) {
new AskCacher();
new Thread(new InfiniteLoop()::loopProcess).start();
}
}
The example only shows how the application is composed, but I was not able to use it to raise an error but I opened my repo as public:
https://github.com/Sick-E/TrioBot
Can anyone please help me?
Thank you.
Tomas

You can replace your code with something like that (no exception handling is required)
Optional.ofNullable(trio.getThirdPair().getBids().firstEntry())
.map(Map.Entry::getKey)
.ifPresent(trio.getTrioAnalysis()::setBidThird);

Related

Sending data within three threads in java, one after another

I wanted to make an elevator system where you send information from the following:
elevator (data) -> scheduler (buffer) -> floor (receive)
The Floor subsystem and the Elevators are the clients in the system; the Scheduler is the server.
when I pressed run some of the issues were:
class elevator is shown below:
package elevator;
import java.util.HashSet;
import java.util.Set;
public class elevator {
public enum State {
MOVING_UP, MOVING_DOWN, STOPPED
}
private int floor;
private State state;
#SuppressWarnings({ })
private Set<Integer> pressedButtons = (Set<Integer>) new HashSet<Integer>();
public elevator() {
state = State.STOPPED;
}
public int getFloor() {
return floor;
}
public void setFloor(int floor) {
this.floor = floor;
pressedButtons.remove(floor);
}
public State getState() {
return state;
}
public void setState(State s) {
state = s;
}
public boolean isMoving() {
return state == State.MOVING_UP || state == State.MOVING_DOWN;
}
public void buttonPressed(int i) {
pressedButtons.add(i);
}
public Set<Integer> getButtons() {
return pressedButtons;
}
public String toString() {
return "Floor: " + floor + "\n" + "\t State: " + state + "\n";
}
}
Exception in thread "Thread-0" java.lang.ClassCastException: class elevator.HashSet cannot be cast to class java.util.Set (elevator.HashSet is in unnamed module of loader 'app'; java.util.Set is in module java.base of loader 'bootstrap')
at elevator.elevator.(elevator.java:21)
at elevator.elevatorExchange.retrieveData(elevatorExchange.java:30)
at elevator.elevatorExchange.run(elevatorExchange.java:19) at java.base/java.lang.Thread.run(Thread.java:835)
Thread 1: elevator
package elevator;
import java.util.concurrent.BlockingQueue;
public class elevatorExchange implements Runnable{
private BlockingQueue<elevator> messages;
public elevatorExchange(BlockingQueue<elevator> messages) {
this.messages = messages;
}
#Override
public void run() {
try {
elevator elevatorData = retrieveData();
messages.put(elevatorData);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private elevator retrieveData() throws InterruptedException {
Thread.sleep(5000);
elevator elevatorData = new elevator();
return elevatorData;
}
}
Thread 2: Scheduler. Scheduler is only being used as a communication channel from the Floor thread to the Elevator thread
package scheduler;
import java.util.concurrent.BlockingQueue;
import elevator.elevator;
public class Scheduler implements Runnable {
private BlockingQueue<elevator> messages;
public Scheduler(BlockingQueue<elevator> messages) {
this.messages = messages;
}
#Override
public void run() {
elevator elevatorData = messages.take();
}
Thread 3: The floor (this will receive it from the scheduler). This is the part I'm struggling with the most, I am trying to make sure the data passed down to the floor is from the scheduler and not the elevator, but my IDE keeps making changes to the data type that's running a lot of exceptions.
package floor;
import java.util.concurrent.BlockingQueue;
import elevator.elevator;
public class FloorReceiver implements Runnable{
private BlockingQueue<elevator> messages;
public FloorReceiver(BlockingQueue<elevator> messages) {
this.messages = messages;
}
#Override
public void run() {
try {
System.out.println("waiting for data from elevator");
elevator elevatorData = messages.take();
System.out.println("data from elevator" + elevatorData);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Testing:
package floor;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import elevator.elevator;
import elevator.elevatorExchange;
public class elevatorToFloorTest {
public static void main(String[] args) {
BlockingQueue<elevator> messages = new ArrayBlockingQueue<elevator>(1);
elevatorExchange retriever = new elevatorExchange(messages);
FloorReceiver receiver = new FloorReceiver(messages);
new Thread(retriever).start();
new Thread(receiver).start();
}
}
This line is the problem:
private Set<Integer> pressedButtons = (Set<Integer>) new HashSet<Integer>();
The exception message tells us that the HashSet is not a java.util.HashSet but is something you wrote:
class elevator.HashSet cannot be cast to class java.util.Set
i.e., the HashSet is defined in the elevator package.
Your HashSet is not implementing the Set interface. If it were, then you would not need the cast. Since it isn't, the cast cannot magically make it work.
It seems the compiler would have told you, except you used #SuppressWarnings. (I'm not sure what the empty list does).
On the other hand, the code for the elevator class that you have now posted shows an import for java.util.HashSet, not a homebrew implementation. This cannot produce the error shown. Have you failed to rebuild everything?

Can't initialise a class instance from JsonNode with reflection

As I mentioned in the title. Here is my code below
import java.lang.reflect.Field;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ObjectNode;
public class ReflectClass {
public Double getDoubleKey() {
return doubleKey;
}
public void setDoubleKey(Double doubleKey) {
this.doubleKey = doubleKey;
}
public Long getLongKey() {
return longKey;
}
public void setLongKey(Long longKey) {
this.longKey = longKey;
}
private Double doubleKey;
private Long longKey;
public ReflectClass(JsonNode node) throws IllegalAccessException {
Field[] fields = this.getClass().getDeclaredFields();
for (Field field : fields) {
if (field.getType().equals(Double.class)) {
field.setDouble(this, node.get(field.getName()).asDouble());
} else if (field.getType().equals(Long.class)) {
field.setLong(this, node.get(field.getName()).asLong());
}
}
}
public static void main(String[] args) throws IllegalAccessException {
ObjectNode jNode = new ObjectMapper().createObjectNode();
jNode.put("doubleKey", 1.0);
jNode.put("longKey", 11L);
new ReflectClass(jNode);
}
}
However when I run the code above, the error like below pops up.
java.lang.IllegalArgumentException: Can not set java.lang.Double field models.weibo.ReflectClass.doubleKey to (double)1.0
I can certainly initialise the class instance by the traditional way like this.doubleKey = node.get("doubleKey").asDouble(). However if there are too many fields in this class, I'd prefer to initialise it through a loop.

CircularFifoQueue doesn't work inside the method, what to use instead?

I need to write the method which let to store always last 10 (the newset) elements and only 10.I have tried to use CircularFifoBuffer.It works perfectly usee like this:
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import org.apache.commons.collections4.queue.CircularFifoQueue;
public class Main {
public static void main(String[] args) {
Queue<Integer> fifo = new CircularFifoQueue<Integer>(3);
fifo.add(11);
fifo.add(22);
fifo.add(33);
fifo.add(44);
fifo.add(55);
System.out.println(fifo); // [33, 44, 55]
But it doesn;t work when used inside the method:
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import org.apache.commons.collections4.queue.CircularFifoQueue;
public class TV {
public int channelNumber = 11;
public int getChannelNumber() {
return channelNumber;
}
public void addToChannelsHistory(int channnelNumber) {
Queue<Integer> fifo = new CircularFifoQueue<Integer>(3);
fifo.add(channnelNumber);
System.out.print(fifo);
}
}
Could you help what to use instead?
You have to use the notion of attribute, a member of your class which is a data, not a method:
public class TV {
private final Queue<Integer> fifo = new CircularFifoQueue<Integer>(3);
public Queue<Integer> getChannelNumbers() {
return fifo;
}
public Integer getChannelNumber() {
return fifo.isEmpty() ? null : fifo.peek();
}
public void addToChannelsHistory(int channnelNumber) {
fifo.add(channnelNumber);
}
public String toString() {
return fifo.toString();
}
public static void main( String[] args ) {
TV tv = new TV();
tv.addToChannelsHistory(11);
tv.addToChannelsHistory(22);
tv.addToChannelsHistory(33);
tv.addToChannelsHistory(44);
tv.addToChannelsHistory(55);
System.out.print( tv );
}
}
Forgive me if I've misunderstood, but as far as I can tell by copying this locally, this works. However, in a Java program, the main method is the entry point into the program. If you aren't instantiating your TV class in the main method, the addToChannelHistory method will never get run. For instance, this works for me:
public class TV {
public int channelNumber = 11;
public int getChannelNumber() {
return channelNumber;
}
public void addToChannelsHistory(int channnelNumber) {
Queue<Integer> fifo = new CircularFifoQueue<Integer>(3);
fifo.add(channnelNumber);
System.out.print(fifo);
}
public static void main(String[] args) {
TV tv = new TV();
tv.addToChannelsHistory(11);
tv.addToChannelsHistory(22);
tv.addToChannelsHistory(33);
tv.addToChannelsHistory(44);
tv.addToChannelsHistory(55);
}
}
Running that program should print out 33 to the console.

Non-static variable this when putting into a static map

I have a static HashMap to which I'm adding a new item like so:
public static void addSession(Session session) {
if(!map.containsKey(session)){
map.put(session, new SessionThread(session));
}
}
SessionThread is declared locally like so:
public class SessionThread implements Runnable {
That map.put line has a compile error of non-static variable this cannot be referenced from a static context. What is causing the error? this is not referenced anywhere in that method, let alone any non-static members. Everything is either static or in the scope of the method.
Entire class...
package me.scratchjava;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;
/**
* A class for managing websocket threads.
* #author James Smyth <jimsmyth at datafascia.com>
*/
public class SessionManager {
private static HashMap<Session, SessionThread> map = new HashMap<>();
/**
* Called whenever a new websocket is opened.
* #param session
*/
public static void addSession(Session session) {
if(!map.containsKey(session)){
map.put(session, new SessionThread(session));
}
}
public static void removeSession(Session session){
if(map.containsKey(session)){
map.remove(session);
}
}
public static void sendData(Session session, byte[] bytes){
if(map.containsKey(session)){
map.get(session).send(bytes);
}
}
public class SessionThread implements Runnable {
private Session session;
private boolean alive = true;
private final LinkedList<byte[]> messageQueue = new LinkedList<>();
public SessionThread(Session session){
}
#Override
public void run() {
while (alive) {
if(Thread.interrupted()){
alive = false;
return;
}
synchronized (messageQueue) {
while(!messageQueue.isEmpty()){
byte[] msg = messageQueue.poll();
try {
session.getBasicRemote().sendBinary(ByteBuffer.wrap(msg));
} catch (IOException ex) {
Logger.getLogger(SessionManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
public void send(byte[] bytes) {
synchronized (messageQueue) {
messageQueue.add(bytes);
}
}
public void kill(){
alive = false;
}
}
}
Your SessionThread inner class is not static. That means the compiler generates a constructor to capture the value of this, the enclosing class. Since you're trying to create a new SessionThread in a static method, there is no this to capture. Make the class static.
Edit
#directedition: SessionThread should be a static class. Actually it should be a stand alone interface and class.

how to use a synchronized linked hash map correctly

trying to make an lru map by subclassing linked hash map.
the map is run through collections.synchronized.
all usages of the map are surrounded by a synchronized block. the unit test also fails if they are all removed. one would think they are not necessary since the map was run through collections.synchronized.
one thread puts sequential numbers (0,1,2,3 ...) into the map. removals are handled by removed eldest entry. no one else removes entries from the map.
the other thread gets the data from the map.
the following unit test fails usually at "oops". this is when a non zero number shows up in the first position (it should be zero until the map gets full). other strange things can happen like null values in the entry set.
any pointers will be appreciated.
thanks
import static org.junit.Assert.*;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
class LruMap<K,V> extends LinkedHashMap<K,V> {
public LruMap() {
super(defaultMaxSize+1,.75f,true);
maxSize=defaultMaxSize;
}
public LruMap(int arg0) {
super(arg0+1,.75f,true);
maxSize=arg0;
}
public LruMap(int arg0,float arg1) {
super(arg0+1,arg1,true);
maxSize=arg0;
}
public LruMap(int arg0,float arg1,boolean arg2) {
super(arg0+1,arg1,arg2);
if(!arg2)
throw new RuntimeException("you did not construct an lru map!");
maxSize=arg0;
}
public LruMap(Map<K,V> arg0) {
super(arg0);
throw new RuntimeException("you did not construct an lru map!");
}
public boolean removeEldestEntry(Map.Entry<K,V> eldest) {
return size()>maxSize;
}
public final int maxSize;
public static final int defaultMaxSize=2048;
static final long serialVersionUID=0;
}
class Server implements Runnable {
public Server(final int pieces,final int period) {
this.pieces=pieces;
this.period=period;
lruMap=Collections.synchronizedMap(new LruMap<Long,Long>(3*pieces/2));
}
#Override public void run() {
t0=System.currentTimeMillis();
while(piece<stopAtPiece) {
final long dt=System.currentTimeMillis()-t0;
final long target=piece(dt);
System.out.println("adding "+(target-piece+1)+" items");
for(;piece<=target;piece++) {
synchronized(lruMap) {
lruMap.put(piece,piece);
}
}
checkMap(piece,true);
try {
Thread.sleep(100);
} catch(InterruptedException e) {
e.printStackTrace();
break;
}
}
}
Map.Entry<Long,Long>[] checkMap(final long n,boolean print) {
synchronized(lruMap) {
Map.Entry<Long,Long>[] entries=null;
if(lruMap.size()>0) {
final Set<Map.Entry<Long,Long>> entrySet=lruMap.entrySet();
entries=new Map.Entry[entrySet.size()];
entrySet.toArray(entries);
long first=entries[0].getKey();
long last=entries[entries.length-1].getKey();
if(print)
for(Map.Entry<Long,Long> entry:entries)
System.out.print(entry.getKey()+" ");
System.out.println();
if(n<pieces&&first!=0) {
System.out.println("lru: first!=0! "+first);
if(throwWhenfirstIsNotZero) { throw new RuntimeException("oops"); }
}
for(int i=0;i<entries.length-1;i++) {
long p0=entries[i].getKey();
long p1=entries[i+1].getKey();
if(p0>p1)
System.out.println("out of order! "+p0+" "+p1);
else if(p0==p1)
System.out.println("dupicate "+p0+" "+p1);
else if(p0+1==p1)
; // ok
else if(p0+1<p1)
System.out.println("skipped "+p0+" "+p1);
else System.out.println("some case i mssed!");
}
}
return entries;
}
}
public long piece(final long dt) {
return dt/period*pieces+dt%period*pieces/period;
}
public boolean throwWhenfirstIsNotZero=true;
protected long piece;
public long t0;
protected long stopAtPiece=Long.MAX_VALUE;
public final int period;
public final int pieces;
public final Map<Long,Long> lruMap;
}
public class ServerTestCase {
#Before public void setUp() throws Exception {}
#After public void tearDown() throws Exception {}
#Test public void testRun() {
server.stopAtPiece=server.pieces;
server.throwWhenfirstIsNotZero=true;
Thread thread=new Thread(server);
thread.setName("server");
thread.start();
while(thread.isAlive()) {
for(long i=0;i<server.piece;i++)
synchronized(server.lruMap) {
server.lruMap.get(i);
}
}
}
final int period=2*1000;
final int serverPieces=100;
Server server=new Server(serverPieces,period);
}
If you are accessing the collection inside a synchronized(lruMap) block, then you probably don't want to wrap it in Collections.synchronizedMap() - use one or the other. This is because they will probably be using different locks - in fact it's almost certain, because it's extremely unlikely that synchronizedMap() is using synchronized(this) internally.
Also I recommend enter link description here

Categories

Resources