Skip to main content

Posts

Showing posts from August, 2012

How to Stop Thread in Java

  Methods for Stopping a Thread     private volatile Thread thrd; public void stop() { thrd = null; } public void run() { Thread thisThread = Thread.currentThread(); while (thrd == thisThread) { try { thisThread.sleep(interval); } catch (InterruptedException e){ } repaint(); } }   The volatile keyword is used to ensure prompt communication between threads.    A field may be declared as volatile , in which case a thread must reconcile its working copy of the field with the master copy every time it accesses the variable. Moreover, operations on the master copies of one or more volatile variables on behalf of a thread are performed by the main memory in exactly the order that the thread requested.”

Basic Java Interview Questions

1. Why java does not support multiple inheritance ? Answer :- To say why java doesn't support inheritance directly like c++ should be the diamond problem faced by c++,(Diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override it), and B and C have overridden this method differently, then via which class does it inherit: B, or C?) 2. JVM crash is an Error or Exception.? Answer:- JVM crash is an Error as a normal developer as we cannot handle it, but its exception for JVM developer as they can fix it. 3. Print “Hello World!” Without Main Method in Java? Answer:- public class Testing {    static    {        System.out.println("Hello World");    }    public static void main(String[] args)    {    } } 4. Collections in Java? Interfaces Iterable Collection Generic Collections List Set SortedSet N

Toggle Event in Javascript

Toggle Event in Javascript This functions describes how the link toggles from On state to Off state. <script type="text/javascript"> function toggle_visibility(event) {              event = event || window.event;              var target = event.target || event.srcElement;        var id = target.id;        if(id == 'on')        {         document.getElementById("on").style.display = 'none';         document.getElementById("off").style.display = 'block';        }        else if(id == 'off')        {         document.getElementById("on").style.display = 'block';         document.getElementById("off").style.display = 'none';        }     } </script> <body> Copy & Paste the below code inside html page to get toggle effect <a href="#" id="on" onclick="toggle_visibility(event)" style="d