multithreading - Concurrent threads in Vector (Java) -


i designing multi-threaded piece of code includes part several sensors queried (through socket), , data stored first in vector , subsequently written db.

the entire process time sensitive since each sensor updates every few seconds new data. if data not retrieved in time, lost. currently, have vector of (custom sensor data) class stores information obtained , each of sensors.

the plan open thread each sensor (say, 40-50 in total, not want limit number in case more sensors added later) , have access , fill particular (set index of vector) cell of vector.

is such operation on vector allowed , prudent? also, knowing peculiarities of tcp/ip sockets, drastically speed process introducing threads (as opposed to, say, running in single thread)? there better or more elegant way of doing this?

from write seems queue beffer fit; threads push sensor data on queue , later (perhaps thread) can take elements queue , process them. java (at least version 7 , 8) offers different queue implementations, usage in multithreaded environment. turing85 wrote in comment consider usage of thread pool instead of creating thread each sensor.

edit: reading comment seems there tow different kind of problems

how efficiently query sensors (threads, tasks, pools, etc)

form questions seems connecting sensors read data, , must done @ fixed rate each sensor. can use scheduledthreadpoolexecutor , use method scheduleatfixedrate(runnable command, long initialdelay, long period, timeunit unit) runnable object thar read data sensor; must schedule taks each sensor; thread pool size specified in constructor. in order minimize thread number must less can in class reads data. suggest pust data in queue or in map or in set, depends how data sre stuctured. map same vector proposed, instead of using index can use generic key insert data, , don't care of sizing collection.

and how efficiently organize data subsequent database submission

after data in collection can read , process them; can store in database, or check duplicates or whatever need. prefer having 2 different "layers", 1 collects data other 1 process has been collected; putting "interface" between 2 allows design evolve 1 side without touching other one.

note: solution allows lost data, if reason server goes down, data in collection has not been processed not available anymore.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -