• Post Reply Bookmark Topic Watch Topic
  • New Topic

Two theads running concurr. using the same method

 
fedai gandjaliyev
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
I'm sending SMS through two phones connected to my PC.
So I have a CService class for each.
On a button click I want to create two threads so that the constructor
takes a CService object as a parameter and starts sending messages.
But each of these theads have to run concurrently and use a method getClientName().
Client Names I store in a Vector. Event if I create two mehtods
getClientName_1() and getClientName_2() they will anyway access the same Vector.
How can I do so that the threads run concurrently and do not conflict with each other using the
getClientName() method?
Thatnk you!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just read from the Vector, two threads accessing it at the same time won't be a problem at all.
 
fedai gandjaliyev
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code will look like this

class SMSSender extends Thread{
SMSSender(CService service){
public void run(){
for(int i=0; i<MyVector.size(); i++)
String clName = getClientName(i); //These are
String clSurname = getClientSurname(i); // read from the Vector
createSMS();
sendSMS();
}
}
}

//on a button click

SMSSender sender1 = new SMSSender(service1);
SMSSender sender2 = new SMSSender(service2);
sender1.start();
sender2.start();
}

Will there be any conflict in this case?
if that doesn trouble you please give explanation!
Thank you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!