Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Threads Accessing Multiple Methods Independently

 
Srikant Venkata
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have 3 different threads (t1,t2,t3) accessing 3 methods method1(), method2() and method3() .

All, I need to know that
1. Is this achievable in a Multithreaded environment?
2. What if the methods are synchronized or non-synchronized?
3. Can I achieve this using ThreadPools?
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Yes. (Of course if the environment wasn't "multithreaded" then you wouldn't have three threads in the first place.)

2. Things might work differently.

3. Sure, there's no reason those three threads couldn't come from a thread pool.
 
Srikant Venkata
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, Do I need to write 3 different run() methods with separate call or Can I achieve in a single run() method ?

-Regards
Srikant
 
Srikant Venkata
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the best approach to achieve this?
Can you provide me an example

 
Debajyoti Kundu
Ranch Hand
Posts: 30
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Srikant,

First of all you can not have more than one run() method. You can have overloaded run methods but they are not part of Thread class. For e.g :

public class Demo extends Thread{
public static void main(String a[]){
Demo d1=new Demo();
Demo d2=new Demo();
Demo d3=new Demo();
d1.start();
d2.start(10); //error
d3.start(10,20); //error
}
public void run(){
System.out.println(Thread.currentThread().getName()+" started");
}
public void run(int x){
System.out.println(Thread.currentThread().getName()+" started");
}
public void run(int x,int x){
System.out.println(Thread.currentThread().getName()+" started");
}
}

this will throw compilation error(at the highlighted lines).

If you want some part of your code be thread protected(i.e only one thread can access that at a time), then better put that in run method or you can also put it in a synchronized method. If a method is declared as synchronized then only one thread can access it at a time.
 
Nomaan Butt
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hi srikant,
hope this piece of code will answer your query. here i have used three inner classes implemeting runnable and all three are getting instantiated in the main method of class MultiThreadClass.



public class MultiThreadClass {

class ThreadA implements Runnable{

public void run(){
method1();
}
}
class ThreadB implements Runnable{

public void run(){
method2();
}
}

class ThreadC implements Runnable{

public void run(){
method3();
}
}
public static void main(String[] args) {

MultiThreadClass MultiThreadClassObject = new MultiThreadClass();
new Thread(MultiThreadClassObject.new ThreadA()).start();
new Thread(MultiThreadClassObject.new ThreadB()).start();
new Thread(MultiThreadClassObject.new ThreadC()).start();

}



public void method1(){
System.out.println(Thread.currentThread().getName()+" in method1()");
}
public void method2(){
System.out.println(Thread.currentThread().getName()+" in method2()");
}

public void method3(){
System.out.println(Thread.currentThread().getName()+" in method3()");
}
}
 
Srikant Venkata
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow!!! Thanks Nomaan, It just works grt.
Thanks all for the quick response.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic