/* Example for thread in java .
* Two types of thread creation can be done
* One- by extend the thread class
* two- by implement runnable
* below I used runnable.
*/
/* Class 1*/
public class TcpServer implements Runnable {
public static void main(String[] args) {
/* Creating instances for Class 1 and class 2 */
TcpServer ser1=new TcpServer();
TcpServer ser2=new TcpServer();
arun an=new arun();
/* Declaring thread for instance */
Thread t1=new Thread(ser1);
Thread t2=new Thread(ser2);
Thread t3=new Thread(an);
/*
* Thread can be created for any number of instances
*Thread t2=new Thread(ser2);
*/
/*thread started */
t1.start();
t2.start();
t3.start();
}
public void run() {
first();
}
public void first(){
for (int i = 0; i < 100; i++) {
System.out.println("Thread First "+i );
}
}
}/* end of class 1 */
/* Class 2 */
class arun implements Runnable{
public void run() {
second();
}
public void second(){
for (int i = 0; i < 100; i++) {
System.out.println("Thread Second "+i);
}
}
}/* End of class 2 */
No comments:
Post a Comment