ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
info       Did you received any Funny E-Mails from your Friends and like to share with rest of our friends? Yeah!! you can post that stuff   HERE
Google
 
Categories  >>  Software  >>  Core Java  >>  Java J2EE  >>  Java Related
 
 


 

 
 Core Java interview questions  Core Java Interview Questions
 Advanced Java interview questions  Advanced Java Interview Questions
 Swing interview questions  Swing Interview Questions
 EJB interview questions  EJB Interview Questions
 Servlets interview questions  Servlets Interview Questions
 Struts interview questions  Struts Interview Questions
 JDBC interview questions  JDBC Interview Questions
 JMS interview questions  JMS Interview Questions
 SunOne interview questions  SunOne Interview Questions
 J2EE interview questions  J2EE Interview Questions
 Weblogic interview questions  Weblogic Interview Questions
 Websphere interview questions  Websphere Interview Questions
 Java Networking interview questions  Java Networking Interview Questions
 Java J2EE AllOther interview questions  Java J2EE AllOther Interview Questions
Question
Write java code to print "Hello how are you"
Thread1 should have "Hello"
Thread2 should have "how are you"
both the threads should start at the same time
 Question Submitted By :: Guest
I also faced this Question!!     Rank Answer Posted By  
 
  Re: Write java code to print "Hello how are you" Thread1 should have "Hello" Thread2 should have "how are you" both the threads should start at the same time
Answer
# 1
class Callme 
{
		synchronized void call(String msg) 
		{
				System.out.print( msg 
+ " ");
			try 
			{
				Thread.sleep(1000);
			} 
			catch(InterruptedException e) 
			{
				System.out.println
("Interrupted");
			}
			
		}
}
class Caller implements Runnable 
{
		String msg;
		Callme target;
		Thread t;
		public Caller(Callme targ, String s) 
			{
				target = targ;
				msg = s;
				t = new Thread(this);
				t.start();
			}
		public void run() 
			{
				target.call(msg);
			}
}
class Synch 
{
	public static void main(String args[]) 
		{
			Callme target = new Callme();
			Caller ob1 = new Caller
(target, "Hello");
			Caller ob2 = new Caller
(target, "How are you");
// wait for threads to end
			try 
			{
				ob1.t.join();
				
				ob2.t.join();
			}
			catch(InterruptedException e) 
			{
				System.out.println
("Interrupted");
			}
		}
}


The above program will print Hello how are you with two 
diff thread
 
Is This Answer Correct ?    6 Yes 2 No
Tathagata
 

 
 
 
Other Core Java Interview Questions
 
  Question Asked @ Answers
 
transaction attributes ?  2
Which containers use a FlowLayout as their default layout?  3
Which containers may have a MenuBar?  1
How does serialization work Ordain-Solutions2
what is the use of servlet engine? Photon2
If set accepts only one argument then how can it compare two objects to avoid duplicates  2
garbate collector(GC)?  4
Why null interfaces are used in Java?  2
If all the methods in abstract class are declared as abstract then what is difference between abstract class and in interface? Synechron4
What services that container provides?  1
what is private constructor?what are the uses of writing private constructor in our program?  5
Explain the difference between getAppletInfo and getParameterInfo?  1
what is meant by serialization?  6
What is a method ?  2
Can you call a private data from an inner class?  3
When garbage collector invokes object?s finalize() method?  2
What is an abstract class? Wipro5
Difference between the paint() and repaint() methods?  1
Difference between abstract class and Interfaces? Sai-Softech5
What is a thin-client application? Adobe3
 
For more Core Java Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com