Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help me with my code about java Threads. My program is a horse race.  RSS feed

 
aj santos
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to compare the variable for each thread java. The program is a horse race simulation using thread. I stored the list of horse in an ArrayList and created a thread for each horse to simulate the race. Each horse has properties like name and age. The track will run inside the run method. During track I like to monitor and compare each horse's/threads distance travelled. Horse speed are generated in random (1- 5 ). The reason for comparing is I like to boost the trailing horse speed. Id like to change the speed of the trailing horse random number from (1-5) to (1-10). I only give boost to the trailing horse

;

//----------------------------------------------------------------------------------

 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to compare the variable for each thread

What problem are you having doing that?  variables that are in scope would be accessible from any thread.  The only problem would be conflicting uses of the variables from different threads.
 
aj santos
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
Is there a way to compare the variable for each thread

What problem are you having doing that?  variables that are in scope would be accessible from any thread.  The only problem would be conflicting uses of the variables from different threads.


I created a thread for each horse. During the race speed of the horse is generated in random number from (1-5). Id like to compare the horses speed during the race to know which one is the trailing horse(horse who is left behind the race let say that this horse is 50 meters away while the other horses are just 10 meters away from the finish line). Once I know  which one is the trailing horse id like to give that horse a boost by changing its speead random generator to (1-10).
 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
speed of the horse is generated in random number
Are those values held in a variable?  Where are those variables defined?
Make sure the variables holding the values to be compared are in scope to all of the code where you want the variables accessible.  local variables are only accessible locally,

Note: The posted code has many formatting problems. 
There are too many hidden }s at the end of statements.
Some of the code within loops is not indented.

These coding problems make the code harder to read and understand.
 
aj santos
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll make the distancetravelled an instance variable. Now my problem is how can I compare the distancetravelled of each thread/horse. So I can know which one is the trailing horse and give that horse a speedboost. Can you help me in my code. I been racking my brain tmfor 3 days now but I can't seem to find a solution for this.
 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can I compare the distancetravelled of each thread/horse

Where are those variables defined?  Are they in scope everywhere you want to access them?

Note: The posted code has many formatting problems.
There are too many hidden }s at the end of statements.
Some of the code within loops is not indented.

These coding problems make the code harder to read and understand.

Be sure to fix these formatting before posting the new version of the code.
 
aj santos
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
how can I compare the distancetravelled of each thread/horse

Where are those variables defined?  Are they in scope everywhere you want to access them?

Note: The posted code has many formatting problems.
There are too many hidden }s at the end of statements.
Some of the code within loops is not indented.

These coding problems make the code harder to read and understand.

Be sure to fix these formatting before posting the new version of the code.


I place the distancetravelled variable in the upper part of the Horse Class. Make it an instance variable. distancetravelled variable holds the distance travelled by each horse during the race.
 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, if distancetravelled is an instance variable, what problems are you having accessing it using a reference to an instance of the Horse class?
There should be a public get method that returns its value
 
aj santos
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup I make distancetravelled an instance variable and make a get method for this variable I updated my code. Now my question is how can I compare the distancetravelled of lets say thread1 to distancetravelled of thread2 and distancetravelled of thread3 and so on. can you give me a sample code on how to compare?
 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can I compare the distancetravelled 

Use the list of Horse class instances to access the distancetravelled variable for each Horse by calling its get method.

NOTE:  The posted code still has formatting problems as I described earlier.  Please fix them.
 
aj santos
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
how can I compare the distancetravelled 

Use the list of Horse class instances to access the distancetravelled variable for each Horse by calling its get method.


can you give me an example code please.
 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NOTE: The posted code still has formatting problems as I described earlier. Please fix them.  I don't try to work with poorly formatted code.

I assume you know how to write a loop that goes through  the items in a list.
Inside that loop, put the logic for what you want to do with the values in the items in the list.
 
aj santos
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:NOTE: The posted code still has formatting problems as I described earlier. Please fix them.  I don't try to work with poorly formatted code.

I assume you know how to write a loop that goes through  the items in a list.
Inside that loop, put the logic for what you want to do with the values in the items in the list.


Sorry Ok i'll fix the format
 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post again when the code has been formatted and when it can be compiled and executed for testing.
 
aj santos
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Post again when the code has been formatted and when it can be compiled and executed for testing.


done. the code is now updated
 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
done. the code is now updated

Sorry, there are still compiler errors in it.  Please fix the errors and post a clean copy.

If you are having problems understanding the errors, copy the full text of the error message and paste it here.
 
aj santos
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
done. the code is now updated

Sorry, there are still compiler errors in it.  Please fix the errors and post a clean copy.

If you are having problems understanding the errors, copy the full text of the error message and paste it here.


it's now updated.
 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, that's better.  I am able to compile and execute the code.
Now, what changes are you trying to make?   I don't see any comments in the code describing what you want to do.
 
aj santos
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Ok, that's better.  I am able to compile and execute the code.
Now, what changes are you trying to make?


Id like to compare the distancetravelled of each thread/horse. In the code the horse speed are generated in random ( 1-5 ). During the race I want to know which one is the trailing horse (let's say that the trailing horse is the one who left behind during the race slow one. Let's say that the trailing horse is still 30 meters away from the finish line while the other horses are just 10 meters away form the finish line. Once I know which one is the trailing horse I will change its speed random number from (1-5) to (1-10). Now I don't know how to do it.
 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When do you want to compare the distancetravelled values for the Horses?
Once every time unit
Or every time a change is made?
 
aj santos
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:When do you want to compare the distancetravelled values for the Horses?
Once every time unit
Or every time a change is made?


The distance between gate to finish line is 50 meter. Lets say we have 5 horses.

Once the horse is 30 meters away from the finish line I want to compare its distancetravelled to the average distancetravelled of other horses. If the average distancetravelled of the 4 other horses is just 10 meters away from finish line. The trailing horse will gain a boost speed times two of the typical random speed (1-5).
 
aj santos
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I like to compare it every time unit
 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like to compare it every time unit

Ok, that sound like a call to the Thread sleep() method could be used inside of a loop that tested each Horse item's values and changed them as needed.  Something like this:
begin loop until end of race
  begin loop through list of Horses
  test values and make changes
  end loop though Horses
  sleep() for time unit
end loop until end race
 
aj santos
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how and where should I place that in my code. Can you give me an example?
 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where should I place that in my code.

Put it it after where all the horses have been started at the end of the main method.

give me an example

The logic would be something like this:
begin loop until end of race   >>>> probably a while loop
begin loop through list of Horses >>> a for loop through the list
test values and make changes   <<< Put the logic here for what you want to do
end loop though Horses
sleep() for time unit
end loop until end race
 
aj santos
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for my question but how do I compare distancetravelled between thread? is it like

list.get(i). getDistanceTravelled()?
 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
list.get(i). getDistanceTravelled()

Yes.  If the list has Horse objects, then that code would access the distanceTravelled for one of the objects in the list.
 
aj santos
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
list.get(i). getDistanceTravelled()

Yes.  If the list has Horse objects, then that code would access the distanceTravelled for one of the objects in the list.


now my question is how can I compare that to ther distancetravelled?
 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can I compare that

The list has all the Horse objects.  You can access any and all of the contents of the list by using the List interface's get() method.

For example to compare the first two elements of the list to see if the first has travelled farther then the second element:
(list.get(0). getDistanceTravelled() > list.get(1). getDistanceTravelled())

Can you explain in detail what you want the code to do? 
What does it want to find in comparing the values?
What is it going to do with what it finds?
 
aj santos
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
how can I compare that

The list has all the Horse objects.  You can access any and all of the contents of the list by using the List interface's get() method.

For example to compare the first two elements of the list to see if the first has travelled farther then the second element:
(list.get(0). getDistanceTravelled() > list.get(1). getDistanceTravelled())


Another question I would like to ask. When I run my code once the horses reached the finish line the rank/place it shows doesn't follow the natural order of ranking like the 1st, 2nd, 3rd and so on. What I'm seeing in my code is like this 1, 2, 2, 4. I notice that whenever there are two or more horses reached the same time at the finish line they shared the same rank then skip the numbers next to it depending on the number of tie horses.  please see the attached image screenshot. What would we the possible cause of it. I just see this problem. 
rank-problem-in-my-code.JPG
[Thumbnail for rank-problem-in-my-code.JPG]
 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you want to change about the displayed results?
Post the current program's output
and an example of what you want to see instead.

What should be displayed for a tie?
 
aj santos
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:What do you want to change about the displayed results?
Post the current program's output
and an example of what you want to see instead.

What should be displayed for a tie?



It's ok to have a tie like the one in the image( there are 3 1st place because they reached the finish line the same time) but what I want is after the three 1st the rank number should 2nd not 4th. It should follow a natural order.
 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect the problem is due to multiple threads accessing the static place variable at the same time. 
The variable should be moved out of the Horse class to another class where the time of finishing the race would be saved with the place.  Every horse finishing at the same time gets the same place value.
Those finishing later would get a higher value.
 
aj santos
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still racking my brain on how to compare each distancetravelled to other horses distancetravelled. How can I do this.


compare horse1 distancetravelled to other horses

then horse2 distancetravelled to other horses

then horse3 distancetravelled to other horse

and so on. I want the comparison every changes in distancetravelled after the hopped in run method
 
Piet Souris
Rancher
Posts: 1913
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you could create a Runnable that compares all your horses on finished and time needed (or travelled distance), and create your CyclicBarrier with this runnable (see the api). Each time when all horses are waiting for the barrier to be released, this runnable is run. It would simplify the code quite a bit.
 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want the comparison every changes in distancetravelled after the hopped in run method

To do it that way (instead of every unit of time) have the code in the Horse class call a method in another class every time it changes the distanceTravelled value.  That called method would need access to the list of Horses so it could compare all their distanceTravelled values.

how to compare each distancetravelled to other horses distancetravelled. How can I do this. 
compare horse1 distancetravelled to other horses

then horse2 distancetravelled to other horses

then horse3 distancetravelled to other horse

and so on.

Yes, something like that should work. Write code using loops to compare all the Horses' values.
 
aj santos
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to ask is there a way to write a race program without using thread.sleep?
 
Piet Souris
Rancher
Posts: 1913
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you explain why you need this Thread.sleep(1000)?
 
aj santos
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:can you explain why you need this Thread.sleep(1000)?



to pause the thread and to see the hopped of each horses every second.


I tried to remove the thread.sleep and the output is like this


let' say 3 horses: horse1 , horse2 and horse3


horse1 started 1st then ended 1st
horse2 started 2nd then ended 2nd
horse3 started 3rd then ended 3rd


there is no overlapping in of hopped between horses. once the thread/horse starts it will continue all the way to finish line. the next horse only starts once the other reached the finish line


There must be something wrong in my code.
 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a way to write a race program without using thread.sleep?

It depends on how you want the timing of the program's output to appear. 
Another scheme for a chain of events would be for the program to move directly from one event to the next without waiting.
I forget what this type of simulation is called.  The program computes the time to the next event and then immediately executes it without a use of Thread.sleep().  It is possible for multiple events to happen at the same time. Threads are not used.
Future events are placed in a queue ordered by time of execution.  The next event is removed and executed.  The execution often creates a new event that goes into the queue.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!