Tuesday 4 August 2015

Java Interview Question

Java Multithreading concept and join() method


You must understand , threads scheduling is controlled by thread scheduler.So, you cannot guarantee the order of execution of threads under normal circumstances.
However if you use join() ,it makes sure that as soon as a thread calls join,the current thread(yes,currently running thread) will not execute unless the thread you have called join is finished.
For example, in your case
ob1.t.join();
This will make sure that the current running thread will run only after t has finished running.
Try this,
class Demo { 
   Thread t = new Thread(
                 new Runnable() { 
                     public void run () { 
                         //do something 
                     } 
                  } 
    ); 
    Thread t1 = new Thread(
                 new Runnable() { 
                     public void run () { 
                         //do something 
                     } 
                  } 
    ); 
    t.start(); // Line 15 
    t.join();  // Line 16 
    t1.start(); 
} 
In the above example, your main thread is executing. When it encounters line 15, thread t is available at thread scheduler. As soon as main thread comes to line 16, it will not execute unless thread t has finished( remember join() as the currently running thread will join to the end of the thread on which join is called).Hence main thread will come to line 17 only when thread t has finished.
So it may appear that t.join will affect thread t1, but it is actually affecting main thread.

What is Daemon thread in java

A daemon thread is a thread, that does not prevent the JVM from exiting when the program finishes but the thread is still running. An example for a daemon thread is the garbage collection.
You can use the setDaemon() method to change the Thread daemon properties.


http://stackoverflow.com/questions/9700871/what-is-difference-between-sleep-method-and-yield-method-of-multi-threading



what is difference between sleep method and yield method of multi threading?


27
down voteaccepted
sleep() causes the thread to definitely stop executing for a given amount of time; if no other thread or process needs to be run, the CPU will be idle (and probably enter a power saving mode).
yield() basically means that the thread is not doing anything particularly important and if any other threads or processes need to be run, they should. Otherwise, the current thread will continue to run.

Synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors: if an object is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods.


What is Base 64 encoding?


When you have some binary data that you want to ship across a network, you generally don’t do it by just converting data into stream of bits and bytes over the network in a raw format. Why? because some media are designed for streaming text only. These protocols may interpret your binary data as control characters which they are not.



No comments:

Post a Comment