Posts

Solving the Producer Consumer Problem in java using wait-notify

The Producer Consumer problem is one of the basic questions asked in an interview testing on Multi-threading concepts. A producer is a process that generates data and adds it to a fixed size shared object which may be a list, a queue, a stack -whatever you may choose. A consumer is a process that will simultaneously consume the data from that shared object. Things get complicated when a consumer starts consuming at a speed greater than that at which the data is being produced. It will then have to deal with an empty shared object. In such type of cases , a consumer has to wait , the producer will notify the consumer when it has added data and the consumer can pick up from then on. There may also be a case when the producer produces at a speed greater than that the consumer can consume , so the shared object is full of data.  In that case, the producer has to wait. When the consumer has consumed some data to create space in the shared object, it can notify the producer, which ca...

The famous FizzBuzz problem

Today I came across the Developer survey results 2019 from stackoverflow.com . One of the questions asked in the survey was whether you have ever been asked to solve the FizzBuzz problem in an interview. I recollected that four years back I had coded this in an online test for an interview. I could write the code for it correctly, but the time I took was more than I should have. I decided to give it a try again today, and I feel this time I completed it in lesser time than in my attempt at the interview, but it was again not too great an improvement. After my code ran successfully, I looked over the internet for solutions posted by other people, in order to gain insights from them. I found my solution took a somewhat different approach. I couldn't find any solution that took the same approach as mine ,so I thought it would be nice to post my version of the solution. For those who don't remember the FizzBuzz problem, here is it : Write a program that prints the numbers ...

The Curious Case Of Strings In Java

Image
img src The most important thing to note about String objects in java is that they are immutable. Immutability means that once the state of an object is defined, it cannot be changed. To make String immutable, all the attributes in String class are declared as final. The attribute 'hash' holding the hashcode is an exception though. Following extract from the String class shows the declaration of the attribute which holds the String's value 1 2 /** The value is used for character storage. */ private final char value []; Because this char array is declared as final, once a String object is assigned a value, it can never be changed. However, the references to a String object are mutable, which means , we can point to another String object using the same reference. Let's look at the following code which explains the above concept : 1 2 3 4 5 6 7 8 9 10 11 12 13 public class TestStrings { public static void ...

What is ConcurrentModificationException , how is it caused and how can it be prevented ? (Part 2- ArrayLists)

In the previous article, we saw with an example of HashMap  how a ConcurrentModificationException is caused. In this article ,we will see how it happens in case of Lists. The basics are the same as in the example of a HashMap. When the structure of a list is modified by adding or removing an element from the list while an iterator is  traversing over it, a ConcurrentModificationException is caused. This is because just like HashMap , an  ArrayList also maintains a count of the times the structure of the list is modified (modCount). When an  iterator is obtained on that list, it creates its own expectedCount which is set to the modCount value . Any  addition or removal from the list will change the modCount.When the iterator compares the modCount with  expectedCount whenever iterator's next() method is called , it throws the  ConcurrentModification exception  if it finds a difference .Such an iterator is said to be fail-fast. When we use Cop...

What is ConcurrentModificationException , how is it caused and how can it be prevented ? (Part 1- HashMaps)

ConcurrentModificationException occurs when a Collection is modified structurally while an iterator is already traversing the Collection. Taking the example of a HashMap,while iterating through the entries of a HashMap, if the internal structure of the HashMap is changed by either adding an element to it or removing an element from it, a ConcurrentModificationException is caused. What happens internally? HashMap maintains an integer variable called modCount. This modCount is nothing but the count of times the HashMap has been modified structurally. If we add an element to the HashMap, the modCount increases. Similarly, if we remove an element from the HashMap, the modCount increases. Interestingly, when we just update an entry, there isn't any change to the modCount. This is because update to an element does not change the internal structure of the HashMap. When we create an iterator for the Entry table of the HashMap, an expectedCount variable is created which is s...

Writing a Web Service Client in Java using Eclipse IDE

Image
The steps involved in writing the Web Service Client will be as follows: a) Locate the service description in the form of WSDL for the web service in question. (Actually the first step is to search UDDI for the services. I am excluding this step here to simplify the process ) b) Once we have the WSDL document, most of the client code is auto-generated using the wsimport tool. c)   Run the client and invoke the web service. Now I will demonstrate the steps to write a Web Service client in Java using Eclipse IDE. This web service client is invoking the web services available at the url  "http://www.webservicex.net ”         1)       Go to the url : http://www.webservicex.net/ws/default.aspx              2)       Choose the web service you want to write the client for. I have chosen Computer Unit converter, which gi...