Malwaregeek
Malwaregeek

Reputation: 2274

Calling functions from threads

I am beginner in java. I have been studying multithreading. I want to create two threads and these two threads must run separate methods concurrently. Here these threads should call sum and diff method and run simultaneously. But I am getting an error, that method should be of thread type. How to achieve it.

class Demo implements Runnable 
{
    void  sum()
    {
      //Some lines of code
    }

    void  diff()
    {
      //Some lines of code
    }        
    public void run ()
    {
       System.out.println("Inside run");
    }

 }



class Test 
{
    public static void main (String []args){
        Demo o = new Demo ();
        Demo o1 = new Demo ();
        Thread th = new Thread (o);
        Thread th1= new Thread(o1);
        th.start();
        th1.start();
        o.th.sum();  // getting error here
        o1.th1.diff(); // getting error here
    }
}

Upvotes: 0

Views: 184

Answers (3)

Ankur Lathi
Ankur Lathi

Reputation: 7836

You are getting this error because you are trying to access the Thread's local variable using the object of the Demo class and you can't call the method directly if you want's it to run it in a separate thread. A new thread will spawn only when you call start() method on thread class and then it will execute the code in run() method.

As per your requirement to create two threads and these two threads must run separate methods concurrently, following code should work.

class Demo implements Runnable 
{
    public void run ()
    {
       //sum() method code
    }
 }

class Demo1 implements Runnable 
{
    public void run ()
    {
    //diff() method code
    }
 }


class Test 
{
    public static void main (String []args){
        Demo o = new Demo ();
        Demo1 o1 = new Demo1 ();
        Thread th = new Thread (o);
        Thread th1= new Thread(o1);
        th.start();
        th1.start();
    }
}

Upvotes: 0

Mike Deck
Mike Deck

Reputation: 18397

First of all you have a compilation error because you're trying to reference the variable th as a field on an object of type Demo. th is not a field, but rather a local variable and can be referenced directly (i.e. without the o. prefix). Second, sum() and diff() cannot be called against an instance of Thread as those methods are not defined by thread, but rather by your own Demo class.

All that being said, these compilation problems aren't even the root issue for this code. Based on your code it seems you have some fundamental misunderstandings about the syntax and structure of Java programs so it might benefit you to go through some entry-level tutorials before trying to tackle concurrent programming. But, for the sake of completeness here is a brief explanation of what you need to do in order to make your program work.

When you call Thread.start() it's going to fork a thread and call the run() method of the Runnable you passed into that thread object's constructor.

In order to call the two different methods you need to create two different classes that implement runnable and put the two method implementations in each of their run methods.

Example:

public class Sum implements Runnable {
   public void run() {
      //Add up your numbers
   }
}

public class Diff implements Runnable {
   public void run() {
      //Subtract numbers
   }
}


public class Test {
   public static void main(String[] args) {
      Thread sumThread = new Thread(new Sum());
      Thread diffThread = new Thread(new Diff());
      sumThread.start();
      diffThread.start();
   }
}

Upvotes: 1

Chetan Kinger
Chetan Kinger

Reputation: 15212

Assuming that you are getting a compilation error, the statement o.th.sum() is incorrect.

The statement o.th will cause the compiler to look for a public static class level field in the Demo class with the name th. Since there is no such field in Demo class, you get an error.

Upvotes: 1

Related Questions