Siddharth Bhargava

Ranch Hand
+ Follow
since Feb 23, 2007
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
3
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Siddharth Bhargava

Stephan van Hulst wrote:You would use these if the actual object you want to work with in your code is different from what you need to serialize the object.

The common example is instance-controlled classes. Other classes are not allowed to call their constructor because it shouldn't be possible to have more instances than the ones you already provide. Java's cloning and serialization mechanisms provide 'magic' ways to create new instances anyway. The writeReplace() and readResolve() methods allow your application to (de)serialize representations of these instances, and then convert between the representation and the controlled instance.

Another example is if you have written a class which enforces its invariants very strictly, which make it difficult for the serialization mechanism to create new instances. Let's say you have written an immutable class with final member variables, and you need to perform validation in the constructor before you can guarantee that the class is in a valid state. Because the serialization mechanism requires you to have a parameterless constructor that can't do validation, your type can't be deserialized. The solution here is to use writeReplace() to replace your object with a mutable representation (usually just a javabean) which gets serialized, and upon deserialization the readResolve() method is called to convert the bean back to your strict type.




Please could you refer me to a practical example to run and understand???
7 years ago

Paweł Baczyński wrote:Hashtable has its method synchronized. Like:Collections.synchronizedMap wraps the collection using synchronized methods. Like:
With regards to thread safety, they both do the same thing.

There is also ConcurrentHashMap but it has a different thread-0sadety policy, Did you have a chance to check it?

code samples from JDK 1.8.0_40



Thanks for your reply. Yes I do know about ConcurrentHashMap. So if anyone asks me about the difference between Hashtable and Collections. syncronizedMap(new HashMap()) then would the above explanation be sufficient ?? are there any more differences which I cant let them know. ?? So basically what we are saying is that Hashtable synchronizes on the method level and Collections.synhronizedMap(new HashMap()) synchronizes at the object (mutex) level. rt. ???
7 years ago
Hi,

I have a question or rather an exercise.

You have two threads, one is printing odd numbers and one is printing even number continuously. Manage them in such a way that they print numbers in sequence.

I understand that this could be done in many ways for e.g. through Semaphore, explicit locks, etc.

I have prepared its solution through wait()/notify()/notifyAll() but its not working. Following is the code:

Assumptions:
1) I am limiting the numbers to 15.
2) Also I am storing the numbers in a List and printing it at the end.
3) I am taking counter as a shared variable.









I have broken my head on this code for several days but couldn't solve this . Please do help me and do let me know what's wrong in this code which I have written. I am having nightmares. Everyday I try to solve this problem and bang my head for several hours in this but to of avail and everyday I feel disappointed. This code would help me in clarifying my concept of wait()/notify()/notifyAll().

Thanks,
Sid.

Hi,

I had quetion about writeRepalce() and readResolve() method in Java Serialization. My limited knowledge about the methods is as per below:

writeReplace()
==============

private Object writeReplace() throws ObjectStreamException { }

This method allows the developer to provide a replacement object that will be serialized instead of the original one.

readResolve()
=============

private Object readResolve() throws ObjectStreamException () { }

It is called after Object is deserialized. It must return an Object that then becomes the return value of the readObject() method. Remember to add readResolve method to all typesafe enumerations in your legacy code and all the classes that follow the "singleton design pattern".


I just know this much about these methods. I don't know about their implementation. I just did rote learning of these 2 methods.

What if I want to explain what these 2 methods do to someone ?? How do I explain them ?? Currently I am not in a position to explain these 2 methods to someone. Also I have searched the net for a simple practical example explaining the working of these 2 methods but I couldn't find any. Please guide me through a simple practical example explaining the working of these 2 methods and provide me a link which gives a pratical example.

Thanks
Sid.
7 years ago

Paul Clapham wrote:I just did a quick look through the API documentation and I see that HashMap allows null keys and null values whereas Hashtable doesn't. Hashtable has an elements() method which returns an Enumeration whereas HashMap doesn't. There could be plenty of other differences, I invite you to look at the docs for yourself.



Please Note I am asking the difference between Hashtable and Collections.synchronizedMap(new HashMap()) and not Hashtable and HashMap. I already know the difference between Hashtable and HashMap.
7 years ago
Hi everyone,

This question might be asked before. What is the difference between Hashtable and Collections.synchronizedMap(new HashMap()) ???

I searched the net also but not able to understand any substantial difference for my knowledge. Please do point me to some link which might help.

Kindly please help me..

Thanks
Sid.
7 years ago

Anurag Verma wrote:best place would be javadocs, did you go through that too? let us know what exactly you didn't get..Doing some hands-on will make stuff more clear, did you try that too?



Yes I did went through the Java docs. What exactly does this line "The wait() method releases the lock prior to waiting and reacquires the lock prior to returning from the wait() method. The wait() method is actually tightly integrated with the synchronization lock, using a feature not available directly from the synchronization mechanism. In other words, it is not possible for us to implement the wait() method purely in Java: it is a native method.." mean ?

Also I could not understand the producer / consumer problem.



What is the purpose of taskQueue.notifyAll(); specifically ? Similarly in the below code:




Could you please explain me the producer / consumer e.g. using wait / notify / notifyAll ? Step-by-step what is happening specifically the code inside the synchronized block and after the while loop.

Thanks and Regards,
Sid
Hi,

I am having a really hard time understanding the concept of wait(), notify() and notifyAll(). I have read the articles in the net as well as the books but I havn't got the concept behind the same. Would someone like to explain me the concept with some easy to understand examples. ?

Thanks and Regards,
Siddharth (Sid)
Hi ,

I am learning the topic ReentrantLock in Java. I am referring the book Core Java by Cay S. Horstmann. I have understood the following uptill now as given in the book:



It guarantees that only one thread at a time can enter the critical section. As soon as one thread lock the lock object, no other thread can get past the lock statement. When other threads call lock, they are blocked until the first thread unlock the lock object.

My Doubt:

1) I am unable to understand a simple example of ReentrantLock though I have searched a lot of examples from the net. I don't want to do rote learning, instead I want to understand the concept behind RenetrantLock with a simple pratical example. Could anyone please help me with the same ?

2) Also help me with some real world example where RenetrantLock are being used.

Thanks
Sid

Paul Clapham wrote:

Siddharth Bhargava wrote:so a+"deal" would make a new string literal "meowdeal"



No, this is the flaw in your thinking. It does indeed make a new String value, but it doesn't make a new String literal. Recall that a String literal appears in your code as a quote followed by a series of characters followed by a quote; your example contains three such literals.

And yes, it's true that your example does contain a String literal (at line 8) which contains the same series of characters as that String value created at line 6. But there's nothing which tries to match String values to entries in the String pool. So the literal at line 8 will be in the String pool, as you know, but the value at line 6 won't.



that's what I am asking why does it make a new String value (as in new String object = new String("meowdeal")) and not a string literal ? .... fine that String literal appears in code as a quote followed by series of characters followed by a quote .... but still dosn't a+"deal" make a new string literal "meowdeal" ? let me know which material to read on the net which help me convince and clears my concepts ....
8 years ago
Hi Friends,

I have read "Instantiation with a static factory method" and "Instantiation using an instance factory method" in the latest spring documentation for the 4.2.3.RELEASE. I am unable to understand the example in the documentation ... could someone please explain me these 2 methods with a simple real life example. ...

Thanks
Sid
8 years ago
Hi Friends,

I thought that I have understood the String Pool concepts in Java completely and was confident about it till I came across this program below:



The output of ab==abc comes out to be false ... As I understand that strings are immutable in java .. so a+"deal" would make a new string literal "meowdeal" and ab would point to "meowdeal" ... now we have abc also pointing to "meowdeal" .... hence there should be only one "meowdeal" in string pool and both ab and abc should be pointing to that .... and hence ab==abc should return true ... while its returning false ...

I have been told that a+"deal" makes a new object in the heap similarly to String ab = new String("meowdeal") ... why so ? could anyone explain me this and give me some reference material to read on the same ... I am totally confused and my concepts are totally shattered ...... need some help in regaining my confidence on strings ....
8 years ago

Gamini Sirisena wrote:If there is some problem in getting a connection getConnection is supposed to throw an SQLException

by catching this you can decide on a course of action.. in general terms if there is an opportunity to recover then take than action.. if not you bubble up the Exception.. probably wrapped in an application specific Exception



but can't I check as per below:

Hi Friends,

I have a very basic question in JDBC. I am trying to connect to the database using JDBC.

Query:

Connection con = DriverManager.getConnection("url", "username", "password");

As per the documentation -> If successful then it returns the connection. If not then it gives an SQLException.

Please do let me know how should i check whether I have successfully got the connection to the database or not in my Java program ?

Could I check like this

if(con != null)
out.println("Successfully received the connection ");
else
out.println("Could not get the connection ....");

Thanks and Regards,
Sid
I am not making this for the GUI (like Swing GUI) ... its just a simple event listener for listening for events for a particular group.....