I want to invoke different methods of class at same time.
I am creating 2 classes: Main- this instantiate object of Function and Function; this extends thread and has 2 methods.
Main.java
package ok;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Welcome to Threading");
Function f1 = new Function();
f1.start();
f1.calling();
f1.calling2();
}
}
Function.java
package ok;
public class Function extends Thread {
public void run() {
// TODO Auto-generated method stub
System.out.println("Run");
for(int y=140;y<170;y++){
System.out.println(y);
}
}
synchronized void calling(){
System.out.println("Let the game begin");
for(int y=40;y<70;y++) {
System.out.println(y);
}
}
synchronized void calling2(){
System.out.println("Let the game begin for me");
for(int y=0;y<40;y++) {
System.out.println(y);
}
}
}
How can I make the methods calling()
and calling2()
work at the same time?
If I start a thread it goes to run() call and doesn't have any return type. In my program, I need to have return value as a HashMap
.
Do I need to create two classes which extends Threads and write logic of calling()
, calling2()
in run of those two classes?
Please suggest.