0

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.

vegemite4me
  • 6,621
  • 5
  • 53
  • 79
Death Metal
  • 830
  • 3
  • 9
  • 26
  • 1
    You will need two threads to run two functions at the same time. – JNL Sep 11 '13 at 14:50
  • 1
    possible duplicate of [How to assign different methods to different threads in java](http://stackoverflow.com/questions/8997596/how-to-assign-different-methods-to-different-threads-in-java) – Gray Sep 11 '13 at 14:52

2 Answers2

3

Your calling and calling2 methods are synchronized. The very essence of synchronized keyword is to prevent methods from executing concurrently. So in order to invoke both calling and calling2 in parallel you need to drop the synchronized keyword, at least from the method level.

Then, to invoke two methods at the same time, one Thread object is enough - one invocation can be executed in new thread, the other in the "current" thread, like this:

Thread thread = new MyThread();
thread.start(); // body of run() invoked in a new thread
thread.run();   // body of run() invoked in this thread, concurrently

It's better practice to produce separate Runnable objects for such use-cases, though. This has several advantages over the aformentioned approach:

  • it's more flexible - as the action is decoupled from the execution and encapsulated in a separate object, it becomes more like "data". If you ever feel the need to change computation strategy - e.g. use thread pool - it's trivial to do so with Runnable tasks. Not so with Thread extensions.

  • it's conceptually more sound, as subclassing a Thread suggests specializing its behaviour, while Runnables are more like "task containers" - favor composition over inheritance.

  • it's symmetric - no computation is treated differently, it's trivial to change e.g. which one executes where

Last, you seem to want to return something from your methods. For this, you might use a Future - a java concurrency utility class, representing asynchronous computation. In addition to Runnable advantages, it can return a value and offers features like cancellation and state querying.

Marcin Łoś
  • 3,226
  • 1
  • 19
  • 21
  • @ Marcin Los: Can you elaborate: [b]to invoke two methods at the same time, one Thread object is enough - one invocation can be executed in new thread, the other in the "current" thread. It's better practice to produce separate Runnable objects for such use-cases, though.[/b] – Death Metal Sep 11 '13 at 15:25
  • 1
    @Death Metal Edited to include an example – Marcin Łoś Sep 11 '13 at 15:27
  • @ Marcin Sorry I couldn't do the thread.start(); // body of run() invoked in a new thread thread.run(); However, I have implemented two future, which return me hash tables. My worry is: that won't be too much of overhead, while handling 468 migs of file? – Death Metal Sep 15 '13 at 04:24
1

You have to have the calls for running the methods inside your run() method.

Then you can just call the start() method and both methods will run. But that does not really solve the problem. What you need to do is create two threads and have each of them run their respective threads.

Arash Saidi
  • 2,228
  • 20
  • 36
  • @ Hatori Sanso - Thank you for your reply. Yes, having two methods in Run, won't help me as it won't won't have return value. Can you please elaborate on *is create two threads and have each of them run their respective threads. * – Death Metal Sep 11 '13 at 15:05
  • @ Gray - I have seen that, thank you for sharing the link. I am new to threading and dealing with 500 migs of data, which needs to be organized and then dump it into Database. To organize the data, I want to implement threads. – Death Metal Sep 11 '13 at 15:07