Multiple Threads Accessing Multiple Methods Independently
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
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?
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
So, Do I need to write 3 different run() methods with separate call or Can I achieve in a single run() method ?
-Regards
Srikant
-Regards
Srikant
Srikant Venkata
Greenhorn
Posts: 19
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
What's the best approach to achieve this?
Can you provide me an example
Can you provide me an example
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 14 years ago
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()");
}
}
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Wow!!! Thanks Nomaan, It just works grt.
Thanks all for the quick response.
Thanks all for the quick response.
| Can you smell this for me? I think this tiny ad smells like blueberry pie! The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |








