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                      
Do you have a collection of Interview Questions and interested to share with us!!
Please send that collection to along with your userid / name. ThanQ
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 ?    11 Yes 2 No
Tathagata
 
  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
# 2
We can have a simple program, I have issues with staring 2
threads at the same time, you guys can have a look and let
me know how could i start two threads at the same time.


the code is as follows, we would have a separate class for
each thread, and then we would have a tester class

package threads;

public class Thread1 implements Runnable{

	public void run() {
		System.out.println("Hello how are you");
	}
}

package threads;

public class Thread2 implements Runnable{

	public void run() {
		System.out.println("Hello");
	}
}



then this would be the tester class


package threads;

public class Thread_test {

	public static void main(String[] args) {
		Thread thread1 = new Thread(new Thread1());
		Thread thread2 = new Thread(new Thread2());
		thread1.start(); /* starting the two threads at the same
time, Any suggestions */  
		thread2.start();
		
	}
}
 
Is This Answer Correct ?    0 Yes 4 No
Nil
 
 
 

 
 
 
Other Core Java Interview Questions
 
  Question Asked @ Answers
 
What happens when a main method is declared as private? Sun-Microsystems8
can we have virtual functions in java? Wipro7
whays is mean by inner class? HCL3
suppose string s1="rajnish"; string s2="bhaskar"; then what will be happend ? Fidelity6
What is meant by JVM ?  3
What is 'finally' method in Exceptions?  4
Why const and goto are reserved keyword in java?  1
How Array List can be Serialized. Polaris5
What enableEvents() method do?  1
What is an abstract class? Wipro5
what are the methods of an object class?  3
What is Session and cookies?Explain in detail with an example? CTS2
Are nested try statements are possible?  2
What is the difference between Trusted and Untrusted Applet ? IBM1
How to print nodes of a Binary tree? TCS1
What is the minimum and maximum length of an identifier? TCS6
In the HashMap, we know the values but we dont know the key, then how can we get the key from HashMap ????? Kanbay3
When we will use an Interface and Abstract class?  3
what Data encapsulation in java?  4
Why can't we make jsp as a controller and action servlet in struts? TCS7
 
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