Link to home
Start Free TrialLog in
Avatar of gattyn
gattyn

asked on

Multiple Threads

Thread 1 is ArrayList which holds car objects
Thread 2 is Arraylist which holds cars objects

I have 2 threads running 2 seperate classes which in turn hold an Arraylist of objects. I need some way of comparing the objects in the seperate threads.
Any ideas ?
How do i stop each thread and compare the 2 objects?
Avatar of laes_
laes_

could u be more clear
 post a part of code and put in comment what u want to do
for notifying some thing to thread
u use the notify() method
and wait on the other side
have a look this for any lack of information
else  put u want to do in comment
Avatar of gattyn

ASKER

i have a point in each thread where each vehicle goes through a loop the size of the arrayList
when it does this i need to compare a value from thread1 object against thread2 object once this is done i need both threads to start and go to the next object in the array.

So basically i need o stop both threads, somehow compare object 1 in thread1 against object1 in thread2. Then start thread again.
u can implement a new class
public class SynchronizeThread{

  private boolean ok1=false;
  private boolean ok2=false;

 
  public synchronized void notifyAllOk1(boolean ok1,boolean ok2 ) {
    this.ok1=ok1;
    this.notifyAll();
  }
  public synchronized void notifyOk2(boolean ok1,boolean ok2 ) {
    this.ok2=ok2;
    this.notifyAll();
  }  
 
  public synchronized void waitS() {
    try{
      this.wait();
    }catch(InterruptedException i) {
    }
  }
 
  public boolean isOk() {
    return this.ok1&&this.ok2;
  }
}
when u read the object1 => notifyOk1(true);and the object2 => notifyOk2(true);
once the two object are read and compared
u make the test by isOk
u make continue;
t go to the next iteration
etc etc ...
sorry ,

public synchronized void notifyAllOk1(boolean ok1,boolean ok2 )  =>synchronized void notifyAllOk1(boolean ok1)

  public synchronized void notifyOk2(boolean ok1,boolean ok2 )  =>  public synchronized void notifyOk2(boolean ok2 )
after reading object u put waitS for waiting notification from the other thread, untill it reads the object
Avatar of CEHJ
>>I have 2 threads running 2 seperate classes which in turn hold an Arraylist of objects.

Why don't they simply use the same List?
Avatar of gattyn

ASKER

there is an error with code
 public synchronized void notifyAllOk1(boolean ok1,boolean ~ok2~ )  =>synchronized void notifyAllOk1(boolean ok1) saying multiple markers

the values i need to test are
car1.getScore() and car2.getScore()
Threads are aCar and bCar
can u add these to ur code
CEHJ he want to synchronize between them
and go to the next iteration if the two have read the list[i] object, of every thread
Avatar of gattyn

ASKER

they cant be in the same list because they go through different functions and have to be compared against each other
:) :) :)
i want to say replace "public synchronized void notifyAllOk1(boolean ok1,boolean ok2 )"  by "public synchronized void notifyAllOk1(boolean ok1)"

and  "public synchronized void notifyOk2(boolean ok1,boolean ok2 )"  by "public synchronized void notifyOk2(boolean ok2 )"
>>they go through different functions

Do they start out the same? How are you comparing the List objects?
in the loop code u put
aCar.start()
and  and bCar.star()

i assume that u declared SynchronizeThread synchronizethread before
and then u put synchronizethread.waitS()
if (synchronizethread.isOk())
{
compare car1.getScore() and car2.getScore()
continue;//next iteration
}





Avatar of gattyn

ASKER

You shouldnt assume
i am a novice
synchro.waitS();  is telling me its not initialised

if i do
SynchronizeThread synchro = new SynchronizeThread();
if i create a new one in both car classes will it still work
u have to add to the two thread constructor argument SynchronizeThread
and add a member SynchronizeThread
so the thread constructor become
 Car(SynchronizeThread synchronizethread ...){
this.synchronizethread =synchronizethread;
}
there is also an other pb
the iteration in the list
u can add a member in  SynchronizeThread
it depends of ur need
if u want to make caparaison in the loop so u have not to add a member in the thread class
have u other question
(i might leave the office :-( )
 
Avatar of gattyn

ASKER

i want to make comparison in loop then get result and setScore to each object
still cant get it synchronized though
I have a start class as follows
CarA aCarA = new CarA(1,20, 100);
            aCarA.start();
            CarB aCarB = new CarB(1,20, 70);
            aCarB.start();

how do i put the synchronize part in there
u add to the CarA and carB ,in the  constructor, SynchronizeThread synchronizethread
so it becomes new CarA(1,20, 100,synchronizethread);
new CarB(1,20, 70,synchronizethread);
u declare a new synchronizethread in the main class
then u create the two new thread
then
aCarB.start();
aCarA.start();
then u put
synchronizethread.waitS();
to verify that the two thread read scores ..
u put
if(synchronizethread.isOK())
// as u remark there is two member in synchronizethread to verify that we have the notification of the two thread
{
u get result and compare ...
}
...


it is not necessary to put the start in the loop
just before the loop
i hope i was clear
good bye


laes_
hello gattyn

have u achieved the thread application
Avatar of gattyn

ASKER

No still no synchronized properly and cannot access other object
what is the pb
try to be gattyn clear to accelerate the resolution
u create the synchronizethread
SynchronizeThread synchro = new SynchronizeThread();
then
CarA aCara=new CarA(1,20, 100,synchronizethread);
CarB aCarB = new CarB(1,20, 70,synchronizethread);

aCarB.start();
aCarA.start();

synchronizethread.waitS();
have u firstly done this and did it work ?
Avatar of gattyn

ASKER

The method that has to be synchronised iterates 30 times in each class then carries out the rest of the program, the first 30 from both classes are synchronized, but when it comesback to the method they are slightly out but stay exactly the same amount out throughout method. So eventually they are way out..

for (int i = 1; i <= count; i++)                                                                                  for (int i = 1; i <= count; i++)
                        {                                                                                                                            {
                                                            

                                    do calculation in classA                                                         do calculation in classB
                                    
                                    synchro.notifyAOk(true);                                                        synchro.notifyBOk(true);
                                    if (synchro.isOk())                                                                 if (synchro.isOk())
                                     continue;                                                                              continue;
                                    synchro.waitS();                                                                   synchro.waitS();
                                    
when the two thread achieve their job
u make
try {

aCarB.join();

aCarA.join();

} catch (Exception e) { return ;}


OK
Avatar of gattyn

ASKER

That code just terminates thread surely
Did u read the code and question i put above
>>but when it comesback to the method they are slightly out but stay exactly the same amount out throughout method. So eventually they are way out => be more explicit, ie ?
afterevery iteration u put
synchro.waitS();                                                                        synchro.waitS();  
if (synchro.isOk())                                                                 if (synchro.isOk())
{                                                                                       {
synchro.setokA(false);                                                               synchro.setokB(false);
// u add this method to restart the ok1 and ok2 to false
// else after the first iteration it will make continue for every iteration
                         continue;                                                                              continue;
}
                             
Avatar of gattyn

ASKER

Lets say carA 1 is compared against caB 1 and so on for first 30
carA 30 compared aginst carB 30
The first iteration of 30 is fine eg
carA1
carB1
carA2
carB2
carA3
carB3 and so on to 30
I am not actually comparing at moment just printing to screen to see order before tackle objects

next iteration
carA1
carA2
carA3
carB1
carA4
carB2
carA5
carB3
carA6 and so on in that exact pattern
have u seen the last comment i posted
add this modification set ok1 and ok2 to false in both classes
"synchro.setokA(false);"  become "synchro.setok(false,false);"  
like this in the next iteration we will be in the same context as the first
and it must work fine untill the last iteration
so  gattyn
Avatar of gattyn

ASKER

OK this now seems ok for the sync
cheers 2nd bit to be able to get variable from other object in arrayist in other thread
CarA class has ArrayList of cars
CarB class has Arraylist of cars
each in seperate thread
How do i refernece each from the opposite thread and class
ASKER CERTIFIED SOLUTION
Avatar of laes_
laes_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
all this deserves grade B