Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!

Suhaas Mohandos

Greenhorn
+ Follow
since Oct 16, 2012
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
0
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 Suhaas Mohandos

Hi All,

I was reading about ThreadLocal. I understand that ThreadLocal variable means one copy of the variable for every thread. But in the example below I have two variables threadLocal and i. The output when I ran the code which is obviously random is 33 315 23 887. Here using the variable i which is a local variable inside run() method I am able to acheive the same thing which is independent copy for every thread. Then what is the need for a ThreadLocal variable concept?


@Praveen

How come the change is reflected in the "calling method" in example1?


@Ole Sandum
If the first example had been 'value = new int[] {150}' or the second had been 'd.name = "Fifi"' (or, ideally, 'd.setName("Fifi")'), then you would see similar results for both.

1. So if I use new in the called method, the change will not be reflected in the calling method?

2. If you are saying primitives are pass by value and objects are also passed by value in java then give me a fresh example of both(primitive and object) where the change is not reflected in the calling method?

3. When  they say java is pass by value does it mean that changes made in the called method will not affect the calling method?
4 years ago
If I use an array as in the example 1, the change is reflected in the calling method. But if I use example 2, the change is not reflected. Why?

Example 1:



Example 2
4 years ago
Ok. Can I assume that since the JLS does not mention anything regarding cache in the definition of volatile, cache is not used for a volatile variable?
4 years ago
Ok, so if I am not using volatile does cache come in picture?
4 years ago
Ok thanks. So can I conclude that if a variable is declared as volatile then both read and write of that variable happens from main memory and the cache is not updated?
4 years ago
By definition of volatile:

By making a variable volatile using the volatile keyword in Java, application programmer ensures that its value should always be read from main memory and thread should not use cached value of that variable from their own stack.

I want to understand if I don't use volatile keyword why the main memory value of a variable say x is different from it's cached value. In multithreading when exactly the local cache is updated and when main memory will be updated if not using volatile.

Appreciate an example
4 years ago
Hi,

When we create a Spring project, the xml file which contains the beans configuration is placed in src folder saying that src is the classpath. How come src is the classpath?

What is difference between build path and class path?

Thanks
4 years ago
Hi,

I am unable to understand the difference between API and framework. Please guide me.

Regards
Suhaas
4 years ago
Hi,

My question is on what basis I have to override hashcode and equals methods and when to implement the Comparator/Comparable interface.

To rephrase the question how will I decide whether I have to implement hashcode and equals method or I need to implement Comparator/Comparable interface.

Sorry if the question is not clear.

Regards
Suhaas
4 years ago
The Beans.xml is in src folder only.

Please see screenshot
4 years ago
Hi,

I believe I have the correct project structure.

I have created a Java project. Created Beans.xml in src folder and created the packages and classes in src folder.

I have attached a screenshot of the project structure
4 years ago
I am trying out  a basic example of constructor based dependency injection.

I am getting the following error:

Aug 03, 2016 5:45:09 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@29104a: startup date [Wed Aug 03 17:45:09 IST 2016]; root of context hierarchy
Aug 03, 2016 5:45:09 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
Aug 03, 2016 5:45:09 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1a03464: defining beans [spellChecker,textEditor]; root of factory hierarchy
Inside spell checker constructor
Exception in thread "main" java.lang.IllegalArgumentException
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.core.LocalVariableTableParameterNameDiscoverer.inspectClass(LocalVariableTableParameterNameDiscoverer.java:112)
at org.springframework.core.LocalVariableTableParameterNameDiscoverer.getParameterNames(LocalVariableTableParameterNameDiscoverer.java:86)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:193)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1049)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.tutorialspoint.MainApp.main(MainApp.java:9)

Files:






4 years ago
Wow. It worked. But now my question is the setter is taking a local variable as a parameter then how come the property mentioned in the xml is of instance variable?
4 years ago