I am working on multi-threading environment where I have below scenario.
I am creating 5 threads and passing same object into all threads. (just action value is different)
MyClass myclass= new MyClass();
myclass.id("123");
myclass.setAction("Deposit");
MyClass myclass1= new MyClass();
myclass1.id("123");
myclass1.setAction("withdraw");
MyThread t1= new MyThread(myclass) // in my Thread Class I have implimented Runnable interface and over ride run method and from run method i am calling transaction method
MyThread t2= new MyThread(myclass)
MyThread t3= new MyThread(myclass)
MyThread t4= new MyThread(myclass1)
MyThread t5= new MyThread(myclass1)
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
public void transaction(MyClass myclass){
synchronized(myclass.id()){
// I am writing my code
}
}
I have two different objects but id is same, will these threads work in sync manner or I have create two new instance so it will execute in async?
Thank you