//Create a Thread(2nd way)
//import java.lang.Thread;
import java.lang.Runnable; // To implement Runnable class
class NameRunnable implements Runnable { //It will make NameRunnable class use the methods in Runnable class
public int sum,a,b;
public void run() {
System.out.println("NameRunnable running");
System.out.println("Run by "
+ Thread.currentThread().getName()); // Name of the current thread i,e Fed
final int c=funcsum(1,2);
System.out.println(c);
}
public int funcsum(int a,int b) {
sum=a+b;
return sum;
}
}
public class NameThread {
public static void main (String [] args) {
NameRunnable nr = new NameRunnable(); //Creating object of class implemented from Runnable class
Thread t = new Thread(nr); //What does this mean, passing object as parameter to class
t.setName("Fred"); //this will name your thread
t.start(); //this will run the method 'run' in implemented class- NameRunnable
}
}
No comments:
Post a Comment