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
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
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.
Click here to claim your Sponsored Listing.
Category
Contact the business
Telephone
Address
Sec/62
Noida
201301