• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multiple Threads Accessing Multiple Methods Independently

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the best approach to achieve this?
Can you provide me an example

 
Ranch Hand
Posts: 33
1
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow!!! Thanks Nomaan, It just works grt.
Thanks all for the quick response.

 
Does this tiny ad smell okay to you?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic