This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Multiple Threads Accessing Multiple Methods Independently Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Multiple Threads Accessing Multiple Methods Independently" Watch "Multiple Threads Accessing Multiple Methods Independently" New topic
Author

Multiple Threads Accessing Multiple Methods Independently

Srikant Venkata
Greenhorn

Joined: Jun 16, 2011
Posts: 19
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jun 16, 2011
Posts: 19
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

Joined: Jun 16, 2011
Posts: 19
What's the best approach to achieve this?
Can you provide me an example

Debajyoti Kundu
Greenhorn

Joined: Sep 14, 2011
Posts: 23
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

Joined: Oct 19, 2011
Posts: 54

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

Joined: Jun 16, 2011
Posts: 19
Wow!!! Thanks Nomaan, It just works grt.
Thanks all for the quick response.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Multiple Threads Accessing Multiple Methods Independently
 
Similar Threads
even on synchronizing, why o/p cannot be determined??
YES (Yet Another Synchronization) question
Need help regarding serializing multiple Objects by difeerent Threads in a single fil
Quession obout synchronized
Static variable is thread safe?