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
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 |
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 |
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 |
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 |
Can we have more than one package statement in source file ?
What is the use of Getters and Setters method ?
What is the access scope of protected access specifier?
Program to Find the second largest element in an array.
how to print hello world every second till i have pressed enter key ???
What is variable explain with example?
What are the two parts of a conditional statement?
"We cannot create an object of interface but we can create a variable of it". Discuss the statement with the help of an example. (Plz help us to provide immediately.)
How do you define a method?
What is the difference between && and & in java?
what is encapsulation in java? Explain
What is an eror in java?