Forum

Number Of Page View:100,000+/Months
LOGIN |New User

Reply

Starting and Running More Than One Thread
more than one thread means that multiple thread are working here.here we have a one code in which three thread instances are gogn and its create a sngle runnable instance.all thrad instance working in a same runnable instance but each have a unique name.and at the last all thread started by invoking start().
 class NameRunnable implements Runnable {
public void run() {
for (int a = 1; a < 4; a++) {
System.out.println("Run by " + Thread.currentThread().getName());
}
}
}
public class ManyNames {
public static void main (String [] args) {
NameRunnable nr = new NameRunnable(); // Make one Runnable
Thread one = new Thread(nr);
one.setName("tom");
Thread two = new Thread(nr);
two.setName("serah");
Thread three = new Thread(nr);
three.setName("john");
one.start();
two.start();
three.start();
}
}
      when this code is runnning mode the we get :
% java ManyNames
Run by Tom
Run by Tom
Run by Tom
Run by serah
Run by serah
Run by serah
Run by john
Run by john
Run by john
Posted By: Name:Rajesh Kr URL: Starting and Running More Than One Thread