How to create two different thread class inside a main function?



How to create two different thread class inside a main function?..

Answer / lakshmi

The main important property of threads for concurrent and
parallel programming is the possibility for a thread to
execute in parallel with the application's main thread or
with other threads. When the start() method is invoked a
new thread is run, but the process which called the start()
method will continue executing its own thread. Par
consequence, the process can call once again the start()
method and run another thread and so on, creating and
running different threads in parallel. For example,
consider the following program,

class Thready {
public static void main( String args [] ) {
new MyThread("A").start();
new MyThread("B").start();
}
}

class MyThread extends Thread {
String message;

MyThread ( String message ) {
this.message = message;
}

public void run() {
while ( true )
System.out.print( message );
}
}
In this program, the lines


new MyThread("A").start();
new MyThread("B").start();

of the main(String args []) function of the class Thready
start two threads of the class MyThread which are run in
parallel. The thread which is initialized with the message
"A" is started first and the thread which is initialized
with the message "B" is stared immediately after the first
thread was started. But the threads will execute in
parallel since they are infinite loops. Otherwise the first
thread which only prints a message can terminate before the
second thread is started and the parallelism of the two
threads can not occur.

Is This Answer Correct ?    5 Yes 2 No

Post New Answer

More Core Java Interview Questions

Tell me are there implementations for sorting and searching in the java libarary?

0 Answers  


WHAT IS JDK,JVM,CLASS DEFINE ALL?

2 Answers  


How many types of JVM's (OR) Name of the JVM's which are used in Tomcat & Weblogic servers ?

1 Answers   TCS,


In what ways you can handle exception ?

3 Answers  


What is map in java?

0 Answers  


What are accessor methods in java?

0 Answers  


What is the difference between a choice and a list?

0 Answers  


What are the string methods in java?

0 Answers  


What is difference between static variable and global variable?

0 Answers  


Can we serialize singleton class?

0 Answers  


Explain what do you mean by functional overloading in java?

0 Answers   Maveric, Verifone,


what is mean by method signature?

5 Answers   Satyam,


Categories