Java Scrappers
Java Scrappers is a startup group that provides training in Core Java,Advance Java,Java web Frameworks like Spring,Struts,etc.
13/09/2016
Must have App for Android developers ----
https://play.google.com/store/apps/details?id=in.technodroid.swap
25/09/2014
19/09/2014
Now, do a stand-up job at work
The headlines have been coming thick and fast: 'Sitting is killing you', 'Sitting is the new smoking', The war on sitting'. In the ever-changing landscape of health hazards, sitting is the new villain - as a Time magazine gif portrays it, a grim reaper creeping up behind you to lop your head off even as you idly hit 'like' on Facebook posts.
Fitness experts believe the war on sitting can be fought with a simple weapon: by standing more. Of course, ideally all humans should do their jobs while moving around, but since that is not a possible solution in a knowledge economy, the next-best alternative is to stand as much as you can. Or so say the advocates of the 'stand at work' culture, which is slowly filtering into India.
Standing desks - even treadmill desks — are common now in many offices in the West, and India has many early adopters of the standing workstation culture as well. Says Sandeep Ramesh ('Standy' to friends), a 31-year-old manager with Google (Mumbai), "I shifted to standing early this year. I burn three times as many calories just by standing as opposed to sitting all day."
Q) What happens when an Exception occurs in a thread?
In simple words, If not caught thread will die, if an uncaught exception handler is registered then it will get a call back. Thread.UncaughtExceptionHandler is an interface, defined as nested interface for handlers invoked when a Thread abruptly terminates due to an uncaught exception. When a thread is about to terminate due to an uncaught exception the Java Virtual Machine will query the thread for its UncaughtExceptionHandler using Thread.getUncaughtExceptionHandler() and will invoke the handler's uncaughtException() method, passing the thread and the exception as arguments.
public class MyThread implements Runnable {
public void run() {
System.out.println(""+(1/0));
for(int i = 0; i < 10; i++) {
System.out.println("i"+i);
}
}
public static void main(String[] args) {
MyThread mythread = new MyThread();
Thread t1 = new Thread(mythread);
UncaughtExceptionHandler hand = new UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
System.out.println("Thread Name=="+t.getName() + "Exception e::"+e.getMessage());
}
};
t1.setUncaughtExceptionHandler(hand);
t1.start();
}
}
Output : Thread Name==Thread-0Exception e::/ by zero
Concepts of Serialization :
1) Serialization is the translation of your Java object’s values/states to bytes to send it over network or save it.On other hand,Deserialization is conversion of byte code to corresponding java objects.
2) Good thing about Serialization is entire process is JVM independent, meaning an object can be serialized on one platform and deserialized on an entirely different platform.\
3) If you want to serialize any class then it must implement Serializable interface which is marker interface.
Marker interface in Java is interface with no field or methods or in simple word empty interface in java is called marker interface
4) serialVersionUID is used to ensure that same object(That was used during Serialization) is loaded during Deserialization.serialVersionUID is used for version control of object.
5) When you serialize any object and if it contain any other object reference then Java serialization serialize that object’s entire object graph.
6) If you don’t want to serialize any field,then make it trasient.
7) If superclass is Serializable then its subclasses are automatically Serializable.
8) If superclass is not Serializable then all values of the instance variables inherited from super class will be initialized by calling constructor of Non-Serializable Super class during deserialization process.
9) If you don’t want subclass to serializable then you need to implement writeObject() and readObject() method and need to throw NotSerializableException from this methods.
10) You can’t serialize static variables.
Method Overriding with Exception Handling :
There are few things to remember when overriding a method with exception handling. If super class method does not declare any exception, then sub class overridden method cannot declare checked exception but it can declare unchecked exceptions.
1) Example of Subclass overridden Method declaring Checked Exception :
import java.io.*;
class Super
{
void show() { System.out.println("parent class"); }
}
public class Sub extends Super
{
void show() throws IOException //Compile time error
{ System.out.println("parent class"); }
public static void main( String[] args )
{
Super s=new Sub();
s.show();
}
}
As the method show() doesn't throws any exception while in Super class, hence its overridden version can also not throw any checked exception.
2) Example of Subclass overridden Method declaring Unchecked Exception :
import java.io.*;
class Super
{
void show(){ System.out.println("parent class"); }
}
public class Sub extends Super
{
void show() throws ArrayIndexOutOfBoundsException //Correct
{ System.out.println("child class"); }
public static void main(String[] args)
{
Super s=new Sub();
s.show();
}
}
Output : child class
Because ArrayIndexOutOfBoundsException is an unchecked exception hence, overridded show() method can throw it.
If Super class method throws an exception, then Subclass overridden method can throw the same exception or no exception, but must not throw parent exception of the exception thrown by Super class method.
It means, if Super class method throws object of NullPointerException class, then Subclass method can either throw same exception, or can throw no exception, but it can never throw object of Exception class (parent of NullPointerException class).
3) Example of Subclass overridden method with same Exception :
import java.io.*;
class Super
{
void show() throws Exception
{ System.out.println("parent class"); }
}
public class Sub extends Super {
void show() throws Exception //Correct
{ System.out.println("child class"); }
public static void main(String[] args)
{
try {
Super s=new Sub();
s.show();
}
catch(Exception e){}
}
}
Output : child class
4) Example of Subclass overriden method with no Exception :
import java.io.*;
class Super
{
void show() throws Exception
{ System.out.println("parent class"); }
}
public class Sub extends Super {
void show() //Correct
{ System.out.println("child class"); }
public static void main(String[] args)
{
try {
Super s=new Sub();
s.show();
}
catch(Exception e){}
}
}
Output : child class
5) Example of Subclass overriden method with parent Exception :
import java.io.*;
class Super
{
void show() throws ArithmeticException
{ System.out.println("parent class"); }
}
public class Sub extends Super {
void show() throws Exception //Compile time Error
{ System.out.println("child class"); }
public static void main(String[] args)
{
try {
Super s=new Sub();
s.show();
}
catch(Exception e){}
}
}
Difference between Heap and Stack Memory
1) Heap memory is used by all the parts of the application whereas stack memory is used only by one thread of ex*****on.
2) Whenever an object is created, it’s always stored in the Heap space and stack memory contains the reference to it. Stack memory only contains local primitive variables and reference variables to objects in heap space.
3) Objects stored in the heap are globally accessible whereas stack memory can’t be accessed by other threads.
4) Memory management in stack is done in LIFO manner whereas it’s more complex in Heap memory because it’s used globally. Heap memory is divided into Young-Generation, Old-Generation etc.
5) Stack memory is short-lived whereas heap memory lives from the start till the end of application ex*****on.
6) We can use -Xms and -Xmx JVM option to define the startup size and maximum size of heap memory. We can use -Xss to define the stack memory size.
7) When stack memory is full, Java runtime throws java.lang.StackOverFlowError whereas if heap memory is full, it throws java.lang.OutOfMemoryError: Java Heap Space error.
8) Stack memory size is very less when compared to Heap memory. Because of simplicity in memory allocation (LIFO), stack memory is very fast when compared to heap memory.
17/08/2014
Wish u all a very Happy Janmashtami from Java Scrappers Team.
Why ConcurrentHashMap is better than Hashtable and just as good as a HashMap:
ConcurrentHashMap is a pretty ignored class. Not many people know about it and not many people care to use it. The class offers a very robust and fast (comparatively, we all know java concurrency isn’t the fastest) method of synchronizing a Map collection.
I have read a few comparisons of HashMap and ConcurrentHashMap on the web. Let me just say that they’re totally wrong. There is no way you can compare the two, one offers synchronized methods to access a map while the other offers no synchronization whatsoever. What most of us fail to notice is that while our applications, web applications especially, work fine during the development & testing phase, they usually go t**s up under heavy (or even moderately heavy) load. This is due to the fact that we expect our HashMap’s to behave a certain way but under load they usually misbehave.
Hashtable’s offer concurrent access to their entries, with a small caveat, the entire map is locked to perform any sort of operation. While this overhead is ignorable in a web application under normal load, under heavy load it can lead to delayed response times and overtaxing of your server for no good reason.
This is where ConcurrentHashMap’s step in. They offer all the features of Hashtable with a performance almost as good as a HashMap. ConcurrentHashMap’s accomplish this by a very simple mechanism. Instead of a map wide lock, the collection maintains a list of 16 locks by default, each of which is used to guard (or lock on) a single bucket of the map. This effectively means that 16 threads can modify the collection at a single time (as long as they’re all working on different buckets). Infact there is no operation performed by this collection that locks the entire map. The concurrency level of the collection, the number of threads that can modify it at the same time without blocking, can be increased. However a higher number means more overhead of maintaining this list of locks.
Retrieval operations on a ConcurrentHashMap do not block unless the entry is not found in the bucket or if the value of the entry is null. In such a case the map synchronizes on the bucket and then tries to look for the entry again just in case the entry was put or removed right after the get in synchronized mode.
Removal operations do require a bit of overhead. All removal operations require the chain of elements before and after to be cloned and joined without the removed element. Since the value of the map key is volatile (not really, the value of the inner Entry class is volatile) if a thread already traversing the bucket from which a value is removed reaches the removed element, it automatically sees a null value and knows to ignore such a value.
Traversal in a ConcurrentHashMap does not synchronize on the entire map either. Infact traversal does not synchronize at all except under one condition. The internal LinkedList implementation is aware of the changes to the underlying collection. If it detects any such changes during traversal it synchronizes itself on the bucket it is traversing and then tries to re-read the values. This always insures that while the values recieved are always fresh, there is minimalistic locking if any.
Iteration over a ConcurrentHashMap are a little different from those offered by other collections. The iterators are not fail-fast in the sense that they do not throw a ConcurrentModificationException. They also do not guarantee that once the iterator is created it will list/show all elements that are added after its creation. The iterators do however guarantee that any updates or removal of items will be reflected correctly in their behaviour. They also guarantee that no element will be returned more than once while traversal.
In conclusion, give it a try, replace some Hashtable’s in your application with ConcurrentHashMap and see how they perform under load.
Important points on load-on-startup element
1. If value is same for two servlet than they will be loaded in an order on which they are declared inside web.xml file.
2. if is 0 or negative integer than Servlet will be loaded when Container feels to load them.
3. guarantees loading, initialization and call to init() method of servlet by web container.
4. If there is no element for any servlet than they will be loaded when web container decides to load them.
5.The container must guarantee that servlets marked with lower integers are loaded before servlets marked with higher integers.
11/08/2014
Unveiled: New chip that functions like our brain
Inspired by the architecture of the brain, scientists have developed a new kind of computer chip that uses no more power than a hearing aid and may eventually excel at calculations that stump today's supercomputers.
The processor named TrueNorth was developed by researchers at IBM and detailed in an article published on Thursday in the journal Science. It tries to mimic the way brains recognize patterns, relying on dense interconnected webs of transistors similar to the brain's neural networks.
The chip's electronic "neurons" are able to signal others when data — light, for example — passes a certain threshold.
Working parallel, the neurons begin to organize the data into patterns suggesting the light is growing brighter, or changing color or shape.
The processor may thus be able to recognize that a woman in a video is picking up a purse, or control a robot that is reaching into a pocket and pulling out a quarter. Humans are able to recognize these acts without conscious thought, but today's computers and robots struggle to interpret them.
The chip contains 5.4 billion transistors, yet draws just 70 milliwatts of power. By contrast, modern Intel processors in today's personal computers and data centres may have 1.4 billion transistors and consume 35 to 140 watts.
Today's conventional microprocessors and graphics processors are capable of performing billions of mathematical operations a second, yet the new chip system clock makes its calculations barely a thousand times a second. But because of the vast number of circuits working in parallel, it is still capable of performing 46 billion operations a second per watt of energy consumed, according to IBM researchers.
The TrueNorth has one million "neurons" -about as complex as the brain of a bee. "It is a remarkable achievement in terms of scalability and low power consumption," said Horst Simon, deputy director of the Lawrence Berkeley National Laboratory.
He compared the new design to the advent of parallel supercomputers in the 1980s.
The new approach to design, referred to variously as neuromorphic or cognitive computing, is still in its infancy, and the IBM chips are not yet commercially available. Yet the design has touched off a vigorous debate over the best approach to speeding up the neural networks increasingly used in computing.
In recent years, companies like Google, Microsoft and Apple have turned to pattern recognition driven by neural networks to vastly improve quality of some services.
WRITE DATA TO EXCEL FILE USING APACHE POI?
FileInputStream file = new FileInputStream(new File("C:\\testdata.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(0);
Cell searchText3 = sheet.getRow(0).getCell(0);
searchText3.setCellValue("Test Search Keyword");
file.close();
Click here to claim your Sponsored Listing.
Category
Contact the business
Telephone
Address
Sec/62
Noida
201301