Thursday, April 28, 2011

Two threads has to print the odd number by one thread and another to print even number



scenario:Say thread t1 and t2 thread objects created.

Thread t1 has to print only the odd number and the t2 has to print the even number.

Solution :

---------------
public class EvenOddGenTest {

/**
* @param args
*/
public static void main(String[] args) {

NumberGenerator numGenerator = new NumberGenerator();

OddGenerator oddGen = new OddGenerator(numGenerator);
EvenGenerator evenGen = new EvenGenerator(numGenerator);

oddGen.start();
evenGen.start();

}

}
------------------

public class OddGenerator extends Thread {

public NumberGenerator numGen;

public OddGenerator(NumberGenerator numberGen) {
this.numGen = numberGen;
}

public void run() {
int i = 1;
while (i <= 9) {

numGen.printOdd(i);
i = i + 2;
}
}

}

----

public class EvenGenerator extends Thread {

public NumberGenerator numGen;

public EvenGenerator(NumberGenerator numberGen) {
this.numGen = numberGen;
}

public void run() {
int i = 2;
while (i <= 10) {
numGen.printEven(i);
i = i + 2;
}
}
}
------


public class NumberGenerator {

boolean oddPrinted = false;

public synchronized void printOdd(int number) {

while (oddPrinted == true) {
try {
wait();

} catch (InterruptedException e) {

}
}

System.out.println("NumberGenerator.printOdd() " + number);
oddPrinted = true;
notifyAll();

}

public synchronized void printEven(int number) {
while (oddPrinted == false) {
try {
wait();

} catch (InterruptedException e) {

}
}

oddPrinted = false;
System.out.println("NumberGenerator.printEven() " + number);
notifyAll();
}
}

--------
Enjoy.... http://www.coderanch.com/images/smilies/e8a506dc4ad763aca51bec4ca7dc8560.gif

puzzles

http://puzzles.nigelcoldwell.co.uk/

A Collection of Quant Riddles With (some) Answers

Wednesday, April 27, 2011

What is the main difference between pass-by-reference and pass-by-value?


What is the main difference between pass-by-reference and pass-by-value?
Ans:
 Other languages use pass-by-reference or pass-by-pointer. But in Java no matter what type of argument you
pass the corresponding parameter (primitive variable or object reference) will get a copy of that data, which is
exactly how pass-by-value (i.e. copy-by-value) works.
In Java, if a calling method passes a reference of an object as an argument to the called method then the passedin
reference gets copied first and then passed to the called method. Both the original reference that was
passed-in and the copied reference will be pointing to the same object. So no matter which reference you use, you
will be always modifying the same original object, which is how the pass-by-reference works as well.
Add caption
 If your method call involves inter-process (e.g. between two JVMs) communication, then the reference of the
calling method has a different address space to the called method sitting in a separate process (i.e. separate
Java - Fundamentals
41
JVM). Hence inter-process communication involves calling method passing objects as arguments to called method
by-value in a serialized form, which can adversely affect performance due to marshaling and unmarshaling cost.

Tuesday, April 26, 2011

what is the difference between class level lock and object level lock

If a thread T1 enters a method m1 by obtaining the class level lock, does this mean another thread T2 cannot run a different method m2 by obtaining the object level lock?
Ans:


No, it doesn't mean that. The "class level lock" is just a regular lock on a different object, namely SomeClass.class. The "object level lock" locks on this.
Just to make sure I'm following your understanding of the terminology, you're wondering if m1 and m2 can be run concurrently as they are defined below:
public class SomeClass {
    public synchronized static void m1() {
       //do something
    }

    public synchronized void m2() {
       //do something
    }
}
And the answer is yes, m1 and m2 can be run concurrently. It is functionally equivalent to this:
public class SomeClass {
    public static void m1() {
        synchronized (SomeClass.class) {
           //do something
        }
    }
    public void m2() {
        synchronized (this) {
           //do something
        }
    }
}
Since they are synchronizing on completely different objects, they are not mutually exclusive.


Monday, April 25, 2011

What is the Diffrence Between Notify & NotifyAll" method in the Object Class.


What is the Diffrence Between Notify & NotifyAll" method in the Object Class ?
 Ans:
'notifyAll()' it says that its will wake up all the threads waiting on the object and will select a thread to pass control to it

It is partly correct, notifyAll() will notify all the threads and after that all the threads will contest for the monitor lock. The thread that gets the monitor lock will start the work and the rest will be waiting for the monitor lock.
The above difference is pretty big as the threads waiting for monitor will not require a notify() call to start processing but will start processing as soon as they get the monitor lock.
If you take the thread dump and see the state of the threads (although getting this dump at the correct time is somewhat tough),
·  In case of notify() only one thread will get to the RUNNING state and other will be in WAIT state
·  In case of notifyAll() only one thread will get to the RUNNING state and other will be in BLOCKED state

How to throw exception in run() method of Runnable?


I want to know how to throw exception in run() method of interface Runnable. Since there is no throwable exception declared in run() method of interface Runnable in Java API specification.
Ans:
Why would you want to?
I can't see any advantage. Exiting the run method will end the thread. Exiting with an exception will do the same (for instance if you were to throw a RuntimeException).
If you want some other thread to be informed of the error then just pass a message as normal (call a method to alter some state and notify other threads).

Relation between Parent and Child thread in java


It is  possible that a parent thread dies before the child ones?
Explanation:
Say i have a Thread "Parent" in it i am starting another two threads say "child1" and "child2" then both childs are still in running phase and Parent Thread dies.
Ans:

All Threads are 'unrelated', there are no parent and child threads. So once the 'parent' thread kicks off 'child1' and 'child2', then the three threads are independent. They can end in any order and they don't affect the running of other threads. Which means 'parent' can die before 'child1' and 'child2'.

There are some modifications to this. For example, if 'parent' is the last non-daemon thread running (and thus 'child1' and child2' threads are daemon threads), then when 'parent' dies, the JVM starts to shutdown, so the 'child' threads would also die. The last modification would be if you have some synchronizing code that prevents 'parent' from dieing until the 'child' threads die, or that signals the 'child' threads to die when the 'parent' does.