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

Answers were Sorted based on User's Feedback



Write java code to print "Hello how are you" Thread1 should have "Hello" Threa..

Answer / tathagata

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 ?    17 Yes 3 No

Write java code to print "Hello how are you" Thread1 should have "Hello" Threa..

Answer / ratan kumar

class ThreadTest1 implements Runnable
{
public void run()
{
System.out.print("Hello ");
}
}
class ThreadTest2 implements Runnable
{
public void run()
{
System.out.print("How are You");
}
}
public class Test {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws InterruptedException {
Runnable r1=new ThreadTest1();
Runnable r2=new ThreadTest2();
Thread t1=new Thread(r1);
Thread t2=new Thread(r2);
t1.start();
t2.start();

t1.join();
t2.join();
}

}

Is This Answer Correct ?    5 Yes 0 No

Write java code to print "Hello how are you" Thread1 should have "Hello" Threa..

Answer / bulu

// thread one to print "Hello"
class Thr1 extends Thread
{
public void run()
{
System.out.print("Hello ");
}
}

//thread two to print "how are you?"
class Thr2 extends Thread
{
public void run()
{
try{
Thread.sleep(1);
}
catch(InterruptedException e)
{
System.out.println("interrution occure");
}
System.out.println("How are you ?");
}
}

// main thread

public class BuluThread
{
public static void main(String ag[])
{
Thread t1=new Thr1();
Thread t2=new Thr2();

t2.start();
t1.start();
}
}


//file name should "BuluThread.java"

Is This Answer Correct ?    3 Yes 2 No

Write java code to print "Hello how are you" Thread1 should have "Hello" Threa..

Answer / nil

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 8 No

Post New Answer

More Core Java Interview Questions

How do you sort in java?

0 Answers  


what is domain object

1 Answers  


Is string is a data type in java?

0 Answers  


Explain the difference between the public, private, final, protected, and default modifiers?

0 Answers  


What is initial size of arraylist in java?

0 Answers  


What are format specifiers in java?

0 Answers  


How do you override a variable in java?

0 Answers  


What is difference overloading and overriding?

0 Answers   Cyient,


I have an HashMap object, which has with key and value pair. It has 10 keys and values in that object. Now the question is I want insert new key and value in middle or any where in that list but not at the end or at the top. Is it possible or not. If yes how can we achieve this one?

2 Answers   Huawei,


what is default layout of JFrame class?

6 Answers  


what is the difference between AWT and SWING what is the advantage of using swing?

3 Answers  


What is the use of 'super' keyword inside a constructor?

0 Answers   Flextronics, Thomson Reuters, Virtusa,


Categories