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 add an element to a hashset in java?

0 Answers  


What is the exact difference in between Unicast and Multicast object? Where will it be used?

0 Answers  


Is java a virus?

0 Answers  


Can a string be null?

0 Answers  


What will be the default values of all the elements of an array defined as an instance variable?

0 Answers  






Which of these methods belong to Thread & Object class? join, yield, sleep, wait, notify

3 Answers   Ericsson,


This is related to threads. I have a class with synchronized method m1(). Can I create different instances of this class and execute the m1() for different threads?

3 Answers  


Can we have any other return type than void for main method?

0 Answers  


If a variable is declared as private, where may the variable be accessed?

0 Answers  


How to find the given number is a prime number or not by getting input from the user

0 Answers  


What is java english?

0 Answers  


What is supplier in java?

0 Answers  


Categories