Threads and Synchronization general approach [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm coding a multithreading server. I got the server itself, many Client objects (each for every connection, in the Thread-Per-Client design) and one single Protocol instance. The Protocol object decides what do with every message the Client send.
I got ONE Protocol object, that many Client object can access, at the same time. The Protocol object got no variables of its own, but it access other objects.
My question is: Can it cause problem, that many clients are accessing the same object at the same time (giving the object got no variables)

If an object has no data, only methods, it's okay to access it in a multithreaded way. However, in your case, your Protocol object accesses other objects, so while your threads may not be sharing data in the Protocol object itself, they will be sharing those other objects and the data in them. Without proper synchronization, that can cause problems.

Related

What is the best practice to store state variables in java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm new to Java.
Say I want to make a simple game with Swing or whatever. How and where do I store variables like the player's score or progress, for example, so that I can access it from different classes (during the game and before persistence in the database). I like how we use useContext in React. I also used global variables in PHP's sessions.
I highly advice to not use a "global state". I would rather recommend to implement a domain object or service that fetches high scores. This service can then be injected and called where needed.
Using a domain object or a service to access and modify domain data has the benefit that ownership is clearly defined: the business object/service owns the data, and the data is only accessed and modified through the business object/service.

HttpClient class AbstractConnPool becames too big,how to solve it ? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
The class org.apache.http.pool.AbstractConnPool has this map field:
private final Map<T, RouteSpecificPool<T, C, E>> routeToPool;
If I use a lot of proxy ip, the map size will became bigger and bigger, this will cause oom.
How to solve it?
To be honest, I never had this issue working with an Apache HTP Client. Looking at your memory dump at Httpclient out of memory I see "104655 instances of class org.apache.http.pool.AbstractConnPool$1" which is an anon inner implementation of RouteSpecificPool which is the value type of the field you mentioned.
So the question is, how do you use the client? The connections have to be released at some point and unused and/or expired connections will be cleaned up.
Do never keep connections in the pool forever or do a keep alive for connections you do not use any more! Such things in combination with requests to high diverse targets (aka routes). You have to configure timeouts for usages (see also Apache Httpclient Connection not Released)!

When you serialized and deserialized an Object where is saved the status of it? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I know that the objects in Java live on the Heap, but when I start to serialize the object and after to deserialize the status of it is keeps on heap? Or in different place?
Edit:
I mean, how the JVM can recreate a new object on the heap? Just reading the file?
I wanted to know what's happens on the heap, before and after, the JVM destroys an object and after it comes back? To do this action, there is something in memory stored or all the information are on the file serialized and deserialized?
When you serialise an object (I suppose you are using ObjectIn/OutputStream), you are turning a Java object into binary data in a stream. This is stream can go to anywhere. It can go to a web server or a file or something else. Serializing an object to a file is the most common, in my experience.
After the object is serialised, it is stored on your hard disc/SSD. However, there is also an independent copy of your object in memory, until your program finishes. It can't just disappear from memory because you might want to use it again.

What's the purpose of objects in Java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Today I had an interview for test automation in one of the MNC.
They asked me "why do we need to create an object?"
I explained about OOPs concepts with example of individual bank account holders. But he is not convinced. He just need a definition.
What could be a suitable answer for that question?
You require an object to represent state.
At the most simple definition, a class defines behaviour and an instance of a class (an object) represents state.
Of course there are other things like static contexts which can also maintain state, which you can mention also, but above is the clearest answer which I believe they were looking for.
It also always helps to give an example. You could talk about, for example, an Employee class. You would need an object to represent John and another to represent Jane.
I think that this question is kind of generic and does not give much value to an interview. But some generic question should have a generic answer, and here is mine:
We need to create objects in java so we can get instances that have a state inside our application. This allows us to have persistent encapsulated elements that contain any required information, and methods that operate with it.
Just plain basic OOP theory.
There are many reasons why we create a object apart from basic oops
1) To bring up persistent state data to transactional state to perform action (curd and other) and persist back to data storage.(EJB, POJO,etc )
2) Creating handler to serve service and send fluid data across wire like web-service.
3)Stuctural behavior in action.for example you designed a class for a workflow and to make in action state we create a object and serve the behavior example validation , authorization , etc class
All in all to make design time architecture to response based live system

Java: In socket programming if client write different type of Objects how server differentiate these object instances? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Well, my question is simple In socket programming if client write different type of Objects how server differentiate these object instances?
Updated:
Take for example if the client sends a string object how the server is suppose to know that it should cast the receiving bytes into a string object?
When you use a Socket you are using a TCP connection, so there's not any abstraction of the Application layer in this communication.
Remember TCP/IP stack:
++++++++++++++++++++++++++++
+ APPLICATION LAYER +
++++++++++++++++++++++++++++
+ TRASPORT (TCP/UDP) LAYER +
++++++++++++++++++++++++++++
The Socket only knows about TCP headers, so it doesn't know anything about the real type of the data, it only knows about TCP packets.
As Boris told you, the application layer is in charge of this know-how. You can use many options:
Send a header (an application header) that can tell every consumer how to deal with this data
Send a representation of the object. Use a technology-aware format: XML, JSON, YAML, etc...

Categories