Wednesday, 21 October 2015

Java Thread example

public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Second obj = new Second("I am a Tea cup");
Second obj1 = new Second("I am a Coffee cup");
Thread t = new Thread(obj);
Thread t1 = new Thread(obj1);
t.start();
t1.start();
}
}

public class Second implements Runnable {
String str1;
Second(String str){
this.str1=str;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<10;i++){
System.out.println("The value of string is " +str1);
}
    }
}

output:-
The value of string is I am a Tea cup
The value of string is I am a Coffee cup
The value of string is I am a Tea cup
The value of string is I am a Coffee cup
The value of string is I am a Tea cup
The value of string is I am a Coffee cup
The value of string is I am a Tea cup
The value of string is I am a Coffee cup
The value of string is I am a Tea cup
The value of string is I am a Coffee cup
The value of string is I am a Coffee cup
The value of string is I am a Coffee cup
The value of string is I am a Coffee cup
The value of string is I am a Tea cup
The value of string is I am a Coffee cup
The value of string is I am a Tea cup
The value of string is I am a Coffee cup
The value of string is I am a Tea cup
The value of string is I am a Tea cup
The value of string is I am a Tea cup

No comments:

Post a Comment