This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Modern JavaScript for the Impatient and have Cay Horstmann on-line!
See this thread for details.
Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!

Vyacheslav Belenky

+ Follow
since Feb 15, 2009
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Vyacheslav Belenky

Thanks Rob. I think this is a good idea. I'll do that.
1 year ago
Hello. I've cloned a project that was not developed by me so I'm not that familiar with the codebase, yet. Then I've tried to build it using "mvn install". After like 5 minutes of waiting and seeing stacktraces on the screen maven said the build is not successful because tests failed. Note, however, that our Jenkins pipeline build is successful which I'll get to later.

I then started looking into the src/test/java directory and found that there are both integration and unit tests. The integration tests rely on some services (RabbitMQ and MySql) which the original developers were running in Docker containers I'm guessing. These developers are not working with us anymore so I can't ask them.

I would like to restructure the codebase but before I do that I wanted to advice with you guys about the following things:

1. It seems to me that if somebody clones a project and run "mvn install" then it should run successfully to begin with. That's my opinion. However, on the other hand, maybe the developer should follow instructions such as run docker-compose in the file to get the "mvn install" to work (just FYI: there isn't a file at this point).

2. I happen to use IntelliJ IDEA IDE (but I think it shouldn't matter what IDE a developer uses). I right-clicked on the src/test/java directory and clicked to run all tests. Of course, the integration tests failed and in general all tests took very long time to run. I was able to by-pass this issue to run only unit tests by putting the pattern "^(.*)Test$" in the IDEA run configuration which now runs only unit tests.

1. The integration tests have an extension "IT".
2. The integration tests are also located in a sub package that has the name "integration".
3. There are some unit tests that are kind of slow (take like 60 seconds to run).

I will be investigating these long-running unit tests so that's a separate issue.

But my biggest concern here that I want to ask advice on is: should developers have to configure in their IDE to avoid running integration tests. Maybe the integration tests should be in a different directory like for example src/integration/java (just an idea).

Sorry if this is a lot of information. I just want to discuss this issue with others as I'm thinking of a good way to restructure this project. I'm not saying that integration tests don't add value but during development I (and I'm sure other developers, too) want to run unit tests frequently so they have to run super fast. And once in a while to run integration tests because they run very slow.

Also, the Jenkins pipeline should obviously run both integration and unit tests but that's a separate issue.

Thanks ahead of time for feedback.
1 year ago
Thank you Daniel for your reply. I have looked over it and redesigned my code (see below) to stop execution based on a volatile boolean flag. Note that I still signal interrupt to the tasks via the ExecutorService.shutdownNow() method to make sure that the underlying code will not be stuck on a blocking method call for a long time or forever.

Please let me know if it now is better:

Hi. I would like to show some code and ask if my strategy for stopping a multi-threaded program is good or is there a better way.

Here is a class whose instances perform "some work" and eventually begin to fail:

And here is the multi-threaded program:

I found code on this page that when I ran replicated the Memory Consistency Error:

For me, the program never exits. To be honest, I was not aware that concurrency in Java is so scary, in a sense that it's possible that a thread will not see a change in a shared variable that another thread makes.
I am reading the Java Concurrency in Practice book. In the beginning of chapter 3 I've read something that surprised me. It says that the following code:

Might result in the following bad behaviors:

(1) ReaderThread will never see the change the main thread made to the ready variable (setting it to true) and thus will be a forever loop

(2) The output of the program might be 0 instead of 42 because of a reordering phenomenon, that is, first the ready variable will be set to true and after that the number variable will be set to 42

I tried to replicate this by running the following program:

I ran with parameter 500 (500 attempts) on a Linux, Mac, and Windows but couldn't replicate.

I wanted to ask if anybody was ever able to replicate one of these problems.
Hello Yevhen. It's great that you're preparing for the OCA exam. I passed mine recently and I would like to give you some tips that you may find helpful.

Verify code on your computer

In your post you said "Actually answers A and B look right". That's a valid concern that you have but what I would also do in your case is type this code into a file and compile it. You would see that the code compiles and then the next question would be why.

Making the code easier to read

I'd love to hear more questions from you and I just want to ask when you'll be posting code to use indentation and the Code formatting feature because it makes the code easier to read. For example, here's the code that you've posted but with indentation and the Code formatting feature:

Isn't that easier to read

Regarding the answer to your question

Ganesh answered your question but I just want to add something. This used to also confuse me, I used to think that a private instance field should only be accessible within the instance portion of the class (such as instance members and the constructors). However, as Ganesh pointed out, private instance fields are also accessible from static methods of the class, too. And what helped me to remember this is when I searched on google in what case this would be useful. An answer that I found was if you have a static factory method.

You may want to disallow client code from being able to instantiate your class using a constructor by making the constructor private and instead have a static factory method. You may want this if the creation of your class instances is complicated and you'd want a static factory method to take care of this. In this case you'd want your static factory method to have access to the private instance fields.

Here is an example of a class with a static factory method:

As you can see in the above example, it is very useful for a static method to have access to the private instance fields.

Let me know if this is helpful.

Congrats! That's a great score.

Thank you Simon.

Did anything unexpected come up?

Umm, I can't remember that there was something that shocked me. The practice questions in the OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide: Exam 1Z04-808 book were harder, therefore I was prepared good. The authors do say in the book that their questions are deliberately harder.

And on your fast completion  time, did you go back to check everything once you had them all answered or were you confident after your first run?

I did not go back to check any of the answers.
2 years ago
Score: 88%
Date: August 6, 2018
Duration: 1 hour and 15 minutes (out of 2 hours and 30 minutes)

Thank you to Jeanne Boyarsky and Scott Selikoff for their OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide: Exam 1Z04-808 book which I have used to prepare for the exam.

I have a lot of years of experience with Java and I even have an old Sun Certificate, however, it's important to go over a study guide because there are some things on the exam that we usually don't apply at work so you have to know them. Some places in the book I skimmed over to make sure there's nothing that I'm missing and some places I went slow to make sure I understand things well. Definitely, do the practice questions. I started preparing on July 22, 2018, reading the book after work and during the weekends, even on the beach on weekends.

To be honest, I wanted to get this exam out of the way as fast as possible because it is a prerequisite for the OCP exam which I am more excited to start preparing for. I will be using the book, OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide: Exam 1Z1-809, authors of which are the same, Jeanne Boyarsky and Scott Selikoff. This should be fun
2 years ago
Regarding the assignment to a final instance variable in a constructor, I think that maybe it would be easier to think about this if instead of saying it needs to be assigned in a constructor (as one of the options), instead to say "in the construction of an instance".

The reason I think that is suppose you have two constructors which happen to not "interact":

Yes, this code compiles just fine and using the wording "...In a constructor..." sounds fine here.

However, the following code does not compile:

with the following compile error:

$ javac error: variable i might already have been assigned
i = 5;
1 error

The only way this code will compile again is if we do not attempt to assign to the final variable in the single-argument constructor:

So using the wording "in the construction of an instance" seems to me a better option.
Simon, it's clearly a mistake in the test. Notice that in the solutions description it says:

StringBuilder does not implement the equals method, so you cannot compare their values using the equals() either.

I actually have the OCA test scheduled to be taken tomorrow. Hopefully they don't have these kind of mistakes
Suppose you download a jar file, project-a.jar. You know that this jar contains a public class abc.PublicClass which you plan to use in your own project.

It happens that internally to the project-a.jar the abc.PublicClass extends a private package class abc.PackageHiddenClass which contains a protected field.

Here is the code for abc.PackageHiddenClass:

And here's the code for abc.PublicClass:

Now suppose in your own project you write code which creates a variable of type abc.PublicClass and you try to access the protected field:

I understand that it will not compile but the compiler message threw me off a little:

$ javac -cp deps/project-a.jar xyz/
xyz/ error: protectedField has protected access in PackageHiddenClass
1 error

I had this notion that a protected member is "inherited" by a subclass so I expected that the compiler will say that "protectedField has protected access in PublicClass".

Question: should I change the way I think about protected members of a superclass?
Hello. I would like to get advice about my career. I'm working on web applications using java. I develop both front and back -end. I find this not interesting because it's always receive a request, change/query the DB, return a model, and render the page. When I talk to recruiters they ask me am I looking for J2EE or Core Java. It seems to me that I should be aiming at Core Java. I started to study multi-threading, for example, and I plan to study the Collections framework. So my question is: does this make sense?
9 years ago