Multi threading Example

package multithreading;
public class Thread1{
public static void main(String args[]) throws InterruptedException{
myThread t1 = new myThread();
myThread2 t2 = new myThread2();

t1.start();
t2.start();
}
}

//Thread 1

class myThread extends Thread{
String s = “Hi Arjun”;
public void run(){

for(int i=0; i<s.length(); i++){
char c = s.charAt(i);
System.out.println(c);
try{
Thread.sleep(2000);
}catch(InterruptedException ex){
ex.printStackTrace();
}
}
}
}

//Thread 2

class myThread2 extends Thread{
String s2 = “Hi Karan”;
public void run(){
for(int i=0; i<s2.length(); i++){
char c2 = s2.charAt(i);
System.out.println(c2);
try{
Thread.sleep(2000);
}catch(InterruptedException ex){
ex.printStackTrace();
}
}
}
}

Download Netbeans Project of Multithreading example

3 thoughts on “Multi threading Example”

Leave a Reply