shared database for multithreading

can someone help me with the trade-offs and pitfalls of creating a shared database? im building an uber-like project that needs to save a list of drivers and their state -as in free, or occupied in an another ride- across all threads. can someone guide me? its a console app.
24 Replies
JavaBot
JavaBot4mo ago
This post has been reserved for your question.
Hey @HanaBanana! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
ayylmao123xdd
ayylmao123xdd4mo ago
wdym by shared database u mean a db that can be accessed by multiple threads at once transactions will be helpful and locking optimistic or pessimistic depending on the use case
HanaBanana
HanaBananaOP4mo ago
a shared hashmap
ayylmao123xdd
ayylmao123xdd4mo ago
how is it shared tho like threads access it at the same time or something
HanaBanana
HanaBananaOP4mo ago
yeahhh can write and read from it concurrently i just learnt about connection pooling do you have any idea how can i use this in my case?
ayylmao123xdd
ayylmao123xdd4mo ago
its gonna create conflicts tho what if one thread deletes something from the hasmap while the other tries to update something for the same drive r
HanaBanana
HanaBananaOP4mo ago
connection pooling is said to be a solution to this
ayylmao123xdd
ayylmao123xdd4mo ago
connection pooling is for connecting to real databases not accessing a hashmap
HanaBanana
HanaBananaOP4mo ago
yeahhhh i figured.. what are we doing now 🙁
ayylmao123xdd
ayylmao123xdd4mo ago
what exactly do you want lock the whole hashmap or only a specific key and value when one thread is accessing it
HanaBanana
HanaBananaOP4mo ago
good question can i just explain the use case? because im unfamiliar with those terms sorry
HanaBanana
HanaBananaOP4mo ago
Access HashMap by multiple threads concurrently (Threads forum at C...
I have a static HashMap will be shared by multiple threads in a real time applicaiton. Do i need to synchronize list variable inside process method?.
ayylmao123xdd
ayylmao123xdd4mo ago
si
Madjosz
Madjosz4mo ago
Use a ConcurrentHashMap then.
ayylmao123xdd
ayylmao123xdd4mo ago
its probably the simplest solution yea
HanaBanana
HanaBananaOP4mo ago
GeeksforGeeks
ConcurrentHashMap in Java - GeeksforGeeks
Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.
Madjosz
Madjosz4mo ago
Also do you need to persist your data? Do you have multiple processes, possibly on different machines all accessing the same data? Use a database. Do you just need an in-memory cache? Use a HashMap. Does it need to support writes and reads from multiple threads? Use a ConcurrentHashMap.
ayylmao123xdd
ayylmao123xdd4mo ago
multiple threads just know that with a concurrent hash map the buckets might lock some other keys when you are updating one specific key so if you lets say update driver1 theres a chance driver2 and driver3 will be locked for the time of updating driver1 but in your case a concurrent hash map should be fine
HanaBanana
HanaBananaOP4mo ago
yeah i think so too but i will test the usecase once im done coding it cus it will take me time since i dont know these concepts and im trying to avoid using ai
ayylmao123xdd
ayylmao123xdd4mo ago
if you wanna finish the app faster id say first do a single threaded version and then switch to multithreading
HanaBanana
HanaBananaOP4mo ago
its already multithreaded, do u wanna see the repo?
JavaBot
JavaBot4mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
ayylmao123xdd
ayylmao123xdd3mo ago
test
JavaBot
JavaBot3mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?