суббота, 30 июня 2018 г.

Java volatile keyword

According to Java Language Specification 8 section 8.3.1.4:
"As a rule, to ensure that shared variables are consistently and reliably updated, a thread should ensure that it has exclusive use of such variables by obtaining a lock that, conventionally, enforces mutual exclusion for those shared variables.

The Java programming language provides a second mechanism, volatile fields, that is more convenient than locking for some purposes. 

A field may be declared volatile, in which case the Java Memory Model ensures that all threads see a consistent value for the variable" .

Please note, that volatile doesn't imply atomic.  So that if your code is like this:
private volatile int var = 0;
var = var+1;
and you run increment in 2 threads then result won't be 2 in all situations.
In other words volatile fix visibility problems but not race conditions.

I recommend to go by following link https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.4 and take a look at example.

From Concurrent programming in Java 2nd:
  • Declaring a reference field as volatile does not ensure visibility of non- volatile fields that are accessed via this reference. 
  • Similarly, declaring an array field as  volatile does not ensure visibility of its elements. 
  • Volatility cannot be manually propagated for arrays because array elements themselves cannot be declared as volatile. 
Declaring fields as  volatile can be useful when you do not need locking for any other reason, yet
values must be accurately accessible across multiple threads. This may occur when:
  • The field need not obey any invariants with respect to others.
  • Writes to the field do not depend on its current value.
  • No thread ever writes an illegal value with respect to intended semantics.
  • The actions of readers do not depend on values of other non-volatile fields.

пятница, 29 июня 2018 г.

Stages of the programmers interview

Here is a short list of possible stages of the interview.

Phone screening. Usually is conducted by recruiter to assure that your phone is correct and you remember what is written in your resume. :-)
Time spent: 20-30 minutes.

Tet-a-tet meeting or Skype interview. This meeting is usually a technical interview where people try to check what you know and what you don't. Resembles like an oral exam. If you prepared well then you can come through it. But sometimes you are given a task which you need to solve or design a system.
Time spent: 1-1.5 hours.

Technical test over the Internet. As a rule you are given some time for a couple of easy technical tasks or a test with answer options. Many companies have their own platforms for such tests, but as a rule no one is supporting them, but the tests are chosen from a given limited set.

But there are exceptions. Some companies use third party sites which provide tests with different type of difficulty: from the middle one to hardcore.
Time spent: 1-1.5 hours.

Testing task. Usually you are given a code for refactoring or being asked to write a simple implementation of some task like a REST controller. It is not recommended to spend more than 3-4 hours on it. The less the better. Value your time. If necessary you can add something by saying when your task will be reviewed. If you are given a real peace of code and asked to do some "production" task then it is better to deny such offer as it may turned out that a company is doing its job for free.
Time spent: 1-3 hours.

Live coding. The most stressful part. People are watching you and how you code. Imagine how your productivity goes down with this approach? Usually no difficult tasks are given, but it is easy to fail. People try to check how you think, but in my opinion this way allows only to check your stress resistance.
Time spent: 0.5-1 hour.

Meeting with manager. At this stage manager is trying to understand how adequate you are and who you are from a business point of you? Are you a team player?
Time spent: 0.5-1 hour.


Mostly this is it. Usually there are no more then 3 stages including phone interview. This process is
very time consuming for you and a company. So it is better to keep short all the stages as possible.

If we'll talk about time then if we sum up all the timings it will be between 4 hours 20 minutes and 8 hours. Usually not all stages are being conducted. For example live coding is often omitted. This timings doesn't include your questions to the company. So expect to add between 0.5 and 1 hour for your questions and company's answers.

четверг, 28 июня 2018 г.

Java 8 programming interview questions

Hi all.
I decided to share with you some stuff which could be useful for the beginners.

Every time you go to the Java interview you almost always get the familiar set of questions. Here is the list of questions that in my opinion are commonly asked at the interviews.

I won't give answers in this post for now. Feel free to explore them on your own.

  • Class Object in Java. Methods equals, hashCode, toString. Default implementation.
  • StringBuilder and StringBuffer. What is the difference? Compare strings by == and equals. String pool.
  • Collections. Basic interfaces and collections. ArrayList and LinkedList. HashMap default implementation. Difference between HashMap and LinkedHashMap and TreeMap.
  • Threads. Thread class. Interface Runnable. Monitor object. Methods wait, notify, notifyAll. Interface Executor. Synchronized keyword. Volatile keyword. Thread-safe collections. Dining philosophers.
  • Difference between interface and abstract class.
  • Lambda expressions. Default methods in interfaces.
  • Exceptions. Checked and unchecked. Difference.
  • Keyword final on class, method, instance member variable, method argument.
  • Try/catch/finally. Cases when finally block will not get executed. 
  • Basic principles of garbage collector.
  • GoF templates. Examples. Do not mention Singleton. Visitor, Adapter, Builder, Template method, Strategy.
  • Basic SOLID principles.
  • What is TDD?
 One of my favorite question is the following: you've got a class which contains one synchronized and one non synchronized method. What will happen if on one instance of this class will operate two threads, one will call synchronized method the other one will call non synchronized method? Is it possible to occur at once?

Answer: One thread can call synchronized method and another one can call  non synchronized method. Synchronized keyword means that a thread needs to obtain object's monitor first. If there are no such word on a method then nothing stops the second thread to operate on an object.
Usually there are other questions about Spring, Databases, J2EE but let's leave it for now. 

That is all for now. Good luck!