Amar Saikia

Ranch Hand
+ Follow
since Feb 04, 2014
Cows and Likes
Cows
Total received
2
In last 30 days
0
Total given
0
Likes
Total received
12
Received in last 30 days
0
Total given
4
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Amar Saikia

Say task1, task2 and task3 take t1, t2 and t3 time to be completed. Now if threads T1 and T2 are equivalent, it will take total t1+t2+t3 time for any of the threads (T1 or T2) to complete the 3 tasks. Now which thread will finish first will depend on the thread scheduling and you may see sometimes T1 is finishing first and sometimes T2. But total time will be always more or less same.In this scenario, I don't think one should go for multi-threading as we are not gaining anything.

Instead, if task1,task2 and task3 are independent and the order of execution does not matter, one can go for multi-threading to speed up the process. Say, T1 is executing task1 and T2 is executing task3 and whoever finishes first will take task2 to process. In this case total time to finish task1, task2 and task3 will be < (t1+t2+t3).

Campbell Ritchie wrote:

Amar Saikia wrote:. . , I can go with a class say Number which contain the int value. . . ..

Since there already is a class called Number, you should consider a different name.



Thanks for your suggestion. Since I don't know the context, I could not able to come up with any meaningful name. However, I agree, that could not be an excuse for suggesting a wrong name.
How about thinking in terms of 'object responsibilities'?

The first object's responsibility is to make sure its value is always correct, no matter how many threads try to increment/decrement its value.
The second object's responsibility is to increment a number
The third objects's responsibility is to decrement a number
The fourth object's responsibility may be to multiply...and so on...

So, if we go with 'single responsibility principle', I can go with a class say Number which contain the int value. I will provide methods such as increment(), decrement(), multiply() in this Number class and make them synchronized just to make sure about the integrity of the int field.

I will have another class NumberIncrementer which will have a non synchronized method increment(). Similarly, I can have another class NumberDecrementer which will have non synchronized method decrement()...and so on...These classes NumberIncrementer,NumberDecrementer will share a common Number object.

This way, I will have to synchronize only in one class and still will be able to protect its integrity.
This is because the variable "oddTurn" is an instance variable to both classes Even and Odd. As both the classes need to communicate with one another to know whose turn it is, there has to be something which is shared. i.e. changes in one class need to be visible to the other class.

To understand why you are getting the result, you need to know the below facts:

1. String is Immutable
2. There is a difference between "==" and equals() method.

After understanding the above points, I am sure you will be able to answer your question by yourself
4 years ago
Yes. The car has a component in form of Engine.
In a multi-threading environment, only one thread can access a synchronized method at a time. However, a singleton object's method can be accessed by any number of threads simultaneously.
Below is the code snippet of trim() from JDK6.



Now, as the length of str2 before trimming is different from length of str2 after trimming, the trim() as per above implementation returns a substring i.e a new object. So we are getting false.


if java is pass by value and we are sending the copy of c1, then why any change made to that copy resulting into the change in c1



Please note that Java is pass by value and we are sending a copy of the reference variable (c1). Not a copy of the whole object. So, we have one object and is referenced by two references (c1 and a copy of c1). So, when you make any change using any of these references, it will change the value in that object, unless its an immutable object. Hope its clear now
Please have a close look in these below lines.

enum COffeeSize{BIG, HUGE, OVERWHELMED}
CoffeeSize size;
That means your war file with name "MSR" is not deployed in the webapps folder of tomcat server.
Could you please check what is the result of the below URLs?

www.localhost:8085/MSR/
www.localhost:8085/MSR/Registration
It looks to me as the property files containing the values are not present in the correct location. Can you please provide the complete information?
5 years ago

Tushar Goel wrote:So you mean to say when i called doSomething() then "this" was LoggingWidget. So it acquires lock on it and
when super.doSomething() is called then "this" was Widget. Hence it acquires lock on it.



Here the 'this' is a single object throughout the code snippet. But this 'this' object is of type 'LoggingWidget' as well as 'Widget' at the same time. So, once 'this' lock got acquired , one doesn't need to acquire the same lock again separately to enter the synchronized function it can call, be it in the same class or be it in the super class.

I've already seen large code blocks inside JSP's



That means your JSP contains lots of business code(logic) which is not a good practice. In an ideal world, JSP need to be used only for presentation. So, IMO, you can move those logic to your serverside code .
6 years ago
JSP