Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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

What is final class?

0 Answers  


Who developed java?

0 Answers  


what is encapsulation in java? Explain

0 Answers  


How is it possible in java programming for two string objects with identical values not to be equal under the == operator?

0 Answers  


Is java jre still free?

0 Answers  


why is S capital in System.out.println ????

6 Answers  


Which java ide is used the most?

0 Answers  


What is the difference between state-based unit testing and interaction-based unit testing?

0 Answers  


How is treeset implemented in java?

0 Answers  


Why singleton pattern is better than creating singleton class with static instance?

0 Answers  


What are streams in java 8?

0 Answers  


Does treeset use compareto?

0 Answers  


Categories